0

I have a DateTime field in Sharepoint

This is the value that I get in Javascript

"2017-12-06T19:00:00Z"

Now the actual value in SharePoint field is

12/6/2017 11:00 AM

How can I change it to this value irrespective of browser time zone in javascript?

Currently I tried new Date(item.StartTime) which gives me Dec 07 2017 00:30:00 GMT+0530

I also tried

moment(item.StartTime).format("hh:mm A") which gives `"07:12:2017 12:30 AM"` which is again not the actual value
4
  • "2017-12-06T19:00:00Z" is the actual value in the SharePoint field. "12/6/2017 11:00 AM" is the value based on the current web and/or user. Commented Nov 8, 2017 at 14:06
  • no but when I try new Date("2017-12-06T19:00:00Z") its giving a different date Commented Nov 8, 2017 at 14:25
  • I also tried Moment JS Commented Nov 8, 2017 at 14:25
  • Go to your site settings > regional settings (https://site-coll-url/_layouts/15/regionalsetng.aspx) and check if they are correct. If not, update it to the correct time zone and then run the code. Commented Nov 9, 2017 at 6:23

1 Answer 1

0

As stated by Gautam, it is important to take into consideration what timezone has been set in the regional settings of your SP site.

As per my understanding, if the SP timezone is set as 'America/Los_Angeles' then at the time of saving the item SharePoint will convert the date time you entered in ISO string format like this "2017-12-06T19:00:00Z" and save it. While displaying the date time in any list view, SharePoint will automatically convert the ISO string to the timezone set in regional settings ("12/6/2017 11:00 AM" in this case).

Hence to summarize if you want to show the ISO String to user in agreement with what is being displayed in list view, simply convert the ISO string to the timezone set in your site's regional settings.

With the help of moment library it can be done fairly easily as shown below:

var itemDate = moment('2017-12-06T19:00:00Z')
console.log(moment.tz(itemDate,'America/Los_Angeles').format('MM-DD-YYYY HH:MM:SS Z'));
//outputs 12-06-2017 11:00:00 -08:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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