SBSAdmin2008
asked on
Sharepoint Datetime picker does not return correct date
Hi Everyone,
I am trying to customize sharepoint site. Site has a list with a field of type datetime. I am capturing event receivers to customize date and time. But if I edit that field with 02/14/2013 12:00 AM, actual value returned by datetime picker is 02/13/2013 7:00:00 PM. How to fix this?
Please let me know your inputs.
Thanks
I am trying to customize sharepoint site. Site has a list with a field of type datetime. I am capturing event receivers to customize date and time. But if I edit that field with 02/14/2013 12:00 AM, actual value returned by datetime picker is 02/13/2013 7:00:00 PM. How to fix this?
Please let me know your inputs.
Thanks
What do you mean by edit that field? Do you mean when someone selects a value or do you mean when you edit a value in code?
ASKER
When someone selects the value using datetime picker -> ItemUpdating() called
within ItemUpdating() I compare AfterProperties(Changed Value) and properties.ListItem (Original Value). I then take difference of both dates to determine how many days a task moved in past or future. But when I look at the AfterProperties(Chnaged value) its not the one selected by user and so i get error in number of days calculation.
I hope i am clear now. Thank you!
within ItemUpdating() I compare AfterProperties(Changed Value) and properties.ListItem (Original Value). I then take difference of both dates to determine how many days a task moved in past or future. But when I look at the AfterProperties(Chnaged value) its not the one selected by user and so i get error in number of days calculation.
I hope i am clear now. Thank you!
Hello,
As I can see the issue is related to Regional Settings of the SharePoint Site/Server.You need to check if your server that run your code is in the same Time Zone with your SharePoint Site. If it's not the same you will need to set your Time Zone settings in the code before the field update.
Please take a look at this article for TimeZoneInfo class information:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx
You can use ConvertTime Method to convert the local system date to the correct date according to SharePoint Time Zone.
I hope it helps.
As I can see the issue is related to Regional Settings of the SharePoint Site/Server.You need to check if your server that run your code is in the same Time Zone with your SharePoint Site. If it's not the same you will need to set your Time Zone settings in the code before the field update.
Please take a look at this article for TimeZoneInfo class information:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx
You can use ConvertTime Method to convert the local system date to the correct date according to SharePoint Time Zone.
I hope it helps.
ASKER
Thanks svetaye! But if i see Site Actions -> Site Settings -> Site Admin -> Regional settings -> Timezone its same as my server (where code runs)!
Are you using a calculated column to store this value or a numeric or string column with a default value calculation?
ASKER
I am using Date and Time type with default value selected none and format is Date and Time.
Yes, but what type of column are you doing your difference calculation in? Does the value for the difference column stay the same as if it's not being dynamically calculated?
ASKER
OK I am posting the code here...
OnJobStartDate() function is called in
I Job Start Date it using UI
Job Start Date [Before] : 2/16/2013 12:00 AM
Job Start Date [After] : 2/18/2013 12:00 AM
In event log I am getting following
Original Date : 2/16/2013 12:00:00 AM,Changed Date : 2/17/2013 7:00:00 PM,Timespan : 1.19:00:00
So I am getting wrong timespan and so calculation is not correct!
OnJobStartDate() function is called in
void ItemUpdating(SPItemEventProperties properties)
private void OnJobStartDateChanging(SPItemEventProperties properties)
{
DateTime changedJobStartDate = DateTime.Parse(properties.AfterProperties[properties.ListItem.Fields[COL_JOB_START_DATE].InternalName].ToString());
DateTime originalJobStartDate = ((DateTime)properties.ListItem[COL_JOB_START_DATE]);
if (!changedJobStartDate.Equals(originalJobStartDate))
{
SPWeb site = properties.OpenWeb();
SPList jobList = site.Lists[LIST_JOBS];
SPQuery q = new SPQuery();
TimeSpan timeSpan = changedJobStartDate.Subtract(originalJobStartDate);
string msg = "Original Date : " + originalJobStartDate.ToString() +
",Changed Date : " + changedJobStartDate.ToString() +
",Timespan : " + timeSpan.ToString();
WriteToEventViewer(msg, EventLogEntryType.Information, 200);
SPFolder jobFolder = jobList.RootFolder.SubFolders[properties.ListItem.Title];
q.Folder = jobFolder;
SPListItemCollection tasks = jobList.GetItems(q);
foreach (SPListItem i in tasks)
{
DateTime taskStartDate = ((DateTime)i[COL_TASK_START_DATE]);
i[COL_TASK_START_DATE] = taskStartDate.Add(timeSpan);
i.Update();
}
}
}
I Job Start Date it using UI
Job Start Date [Before] : 2/16/2013 12:00 AM
Job Start Date [After] : 2/18/2013 12:00 AM
In event log I am getting following
Original Date : 2/16/2013 12:00:00 AM,Changed Date : 2/17/2013 7:00:00 PM,Timespan : 1.19:00:00
So I am getting wrong timespan and so calculation is not correct!
OK, so the event log is where the calculated value is stored rather than a column within the list. I don't know whether you already walked through the debugger, but it could be possible this line is causing you the problem?
SPList jobList = site.Lists[LIST_JOBS];
I think you need quotes around LIST_JOBS unless it is used as some sort of global string variable.
SPList jobList = site.Lists["LIST_JOBS"];
I don't understand why the code even works at all and why VS isn't flagging this, so I might be missing something, but I've never seen it written the way you did..
SPList jobList = site.Lists[LIST_JOBS];
I think you need quotes around LIST_JOBS unless it is used as some sort of global string variable.
SPList jobList = site.Lists["LIST_JOBS"];
I don't understand why the code even works at all and why VS isn't flagging this, so I might be missing something, but I've never seen it written the way you did..
ASKER
yes LIST_JOBS is
Other then this why do you believe it's a non-standard code? I mean I am trying to just update date fields in child items by difference of dates in parent item(Folder). Yes, I am new to sharepoint customization but the kind of thing I am doing looks very normal.
private const string LIST_JOBS = "Jobs";
Other then this why do you believe it's a non-standard code? I mean I am trying to just update date fields in child items by difference of dates in parent item(Folder). Yes, I am new to sharepoint customization but the kind of thing I am doing looks very normal.
I assume COL_JOB_START_DATE and COL_JOB_START_DATE are global as well.
Seems like this is what's failing
DateTime changedJobStartDate = DateTime.Parse(properties. AfterPrope rties[prop erties.Lis tItem.Fiel ds[COL_JOB _START_DAT E].Interna lName].ToS tring());
What value do you see when you walk through the debugger? You should see a value for that right at the beginning of execution.
Seems like this is what's failing
DateTime changedJobStartDate = DateTime.Parse(properties.
What value do you see when you walk through the debugger? You should see a value for that right at the beginning of execution.
ASKER
OK, I dont have VS Pro (Using VS Express). And it looks like VS Express cannot attach debugger to a w2wp.exe. However I added line suggested by you and got follwing
Original Date : 2/16/2013 12:00:00 AM,Changed Date : 2/17/2013 7:00:00 PM,Timespan : 1.19:00:00,From Server : 2/22/2013 8:34:21 AM
While actual time is 2/22/2013 1:22:00 PM (in System Date and Time dialog box)..how is that possible!
Original Date : 2/16/2013 12:00:00 AM,Changed Date : 2/17/2013 7:00:00 PM,Timespan : 1.19:00:00,From Server : 2/22/2013 8:34:21 AM
While actual time is 2/22/2013 1:22:00 PM (in System Date and Time dialog box)..how is that possible!
I don't know the why at the moment and somebody else might. But I think we're getting somewhere and maybe we don't need to look at your code because this oddity certainly would need to be understood first to gain a stable basis for general debugging of the problem. As far as I can see, your code looks really solid. It's just a shame you can't walk through it in the debugger to make sure.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.