Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm reading date column in one of the list and creating document in another list with same date but strangely dates gets change. For example, Date column value is 4/6/2013 but when setting into another list its changing to 3/6/2013.

Here is how I'm reading the date field from List 1,

if (listItem["RDSDate"] != null) { dt = DateTime.Parse((listItem["RDSDate"].ToString())); }

Here is how setting in list 2,

 lItem["RDSDate"] = dt.Date;
share|improve this question
add comment

2 Answers

First of all, you shouldn't need to use the ToString() method and then parse it back into DateTime. The field should already be stored as DateTime if it is a date field and as such you just need to cast it as such.

if ((DateTime)listItem["RSDate"] != null)
{
    dt = (DateTime)listItem["RSDate"];
}

I cast it in the if/else statement so that the cast exception would be thrown sooner. If you just want it to return a null value if it's not a DateTime object (in this case, I don't think you should do this because that really ought to be a DateTime), you can use if(listItem["RSDate"] as DateTime != null) instead.

Going back the other way (assigning the dt value to the "RSDate" field without casting) should work, or if it doesn't (the dt.Date is in a format that is incompatible with the RSDate field) it should throw an error, not input invalid data.

As to why you're getting the wrong date in there, it might be that somewhere in SP you've got things set to UK date/time values, which if memory serves is DD/MM/YYYY, not MM/DD/YYYY like the US uses. It doesn't matter when you're passing DateTime objects around, but if you convert them into strings you might get errors.

share|improve this answer
    
Thanks John. Even if I use your suggestion when I read from List 1 date column it reads 3/6/2013 where as actual date is 4/6/2013. That's why it's getting wrong in List2 column. It's strange I never see this problem before. –  Rishi Jun 12 '13 at 14:38
    
Is it consistently a day off? What's the date/time on the SharePoint server? –  NotVonKaiser Jun 12 '13 at 14:48
    
Yes for all the docs. Lets say if list item has RDSDate column 04/06/2013, When I read and display it shows 03/06/2013 –  Rishi Jun 12 '13 at 14:52
    
Well, if you're sure it's always a day off, one hack would just be to use the .AddDays(1) method before running whatever other calculation you need on the field. But that's not a really great long-term answer. Is it returning GMT? Depending on what time of day (I think GMT is 9 hours ahead of PDT, for instance), it could be returning yesterday... –  NotVonKaiser Jun 12 '13 at 14:55
    
But I'm not trying Now() where time zone may matters. It's the date saved in the list which I'm trying to read.Does it make any sense to display yesterday date if I created an item today? –  Rishi Jun 12 '13 at 15:05
show 1 more comment

Try the below code,

if (listItem["RDSDate"] != null) { DateTime dt= Microsoft.SharePoint.Utilities.SPUtility.CreateDateTimeFromISO8601DateTimeString(listItem["RSDate"].ToString()); }

and include the below namespace

using Microsoft.SharePoint.Utilities;

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.