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

Experts, Can anyone explain me why I am getting 1 day less From a Date Only picker in Item updating event receiver ... this happens for After properties value.

Basically I am trying to validate whether expiry date field is having value less than current date ...it works when actually date set is less than current date, but it does not allow if expiry date set is current date.This is happening because Expiry date set returns 1 day less as after value in item updating event...

SPListItem item = properties.ListItem;
SPFieldDateTime cf = (SPFieldDateTime)item.Fields["Expiry Date"];
string beforeDataTimeStr = cf.GetFieldValueAsText(item["Expiry Date"]);
DateTime dtBefore = DateTime.Parse(beforeDataTimeStr).ToUniversalTime();
DateTime dtAfter = DateTime.Parse(properties.AfterProperties["Expiry Date"].ToString());
SPWeb web = properties.OpenWeb();
SPTimeZone timeZone = web.RegionalSettings.TimeZone;
dtAfter = timeZone.LocalTimeToUTC(dtAfter);

Above code also returns 1 day less for after value. I have set as GMT+9 Japan for Web and GMT+5:30 for Client.

Does any one has any remedy to it ..so that what appears on form is actually retrieved in event receivers?

share|improve this question
add comment

1 Answer

This is almost certainly a result of your timezone.

When a Date Only picker returns a value, it returns a DateTime, where the time component is midnight for the local timezone - e.g. 2012-01-04 00:00:00.

When this value is converted to UTC time, it can become a value for the previous day. For email, if the local timezone for the above datetime is +2, then UTC time would be 2012-01-03 22:00:00.

Naturally, if you simply look at the date part of the date time, this now appears to be an entire day before. I would suggest you start by looking at the date component of the Expiry date - is that offset by your timezone setting?

SharePoint does store all datetimes in UTC time, so it will convert that local datetime to UTC. I'm not sure, off the top of my head, what timezone the datetimes in the AfterProperties are in.

I had an issue with a similar symptom to this before now which was due to timezones. Oh, also, check that the server's own "Automatically adjust for daylight savings" (as per the link) has been checked too - that could cause the same issue if you're using expiry dates that fall in 'Summertime'.

share|improve this answer
    
Thanks Andy ..I'll look at your suggestions and come back with outcome.. –  DotNetSeeker Jan 5 '12 at 6:11
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.