1

I have the below javascript to retrieve Start date time and an End date time for an event.

while(liEnum.moveNext())
{
    var oLiCal=liEnum.get_current();  
    var localDate =  moment();                               
    var eventDate = oLiCal.get_item("EventDate");
    var eventEndDate = oLiCal.get_item("EndDate");
    var eventAllDayEvent = oLiCal.get_item('fAllDayEvent');
    var eventCompanyWideEvent = oLiCal.get_item('CompanyWideEvent');

    console.log("local date: " + localDate.format('YYYY-MM-DD hh:mm a') );
    console.log("Event Start date: " + moment(eventDate).format('YYYY-MM-DD hh:mm a'));
    console.log("Event End date: " + moment(eventEndDate).format('YYYY-MM-DD hh:mm a'));

The calendar has the following date and times of an event:

enter image description here

The console log shows the following:

enter image description here

As we can see the date start and end times retrieved via javascript is 1 hour behind. Can any one please tell why that would be the case, and how can I get the exact date time from the server.

Much Obliged.

3
  • can you share moment() function Commented Sep 8, 2014 at 16:43
  • 1
    moment is a js library that I'm using and link for this library is, momentjs.com. If I hover on the variables eventDate and eventEndDate by start debugging in IE. I get the hours 1 hour behind then that of the calendar item. Commented Sep 8, 2014 at 17:11
  • I had similar issues and documented them here yetanothersharepointblog.wordpress.com/2013/07/14/…
    – RussGove
    Commented Sep 10, 2014 at 0:44

1 Answer 1

6

SharePoint displays date/time according to TimeZone in Regional Settings but returns in UTC format in JSOM.

Using SP.TimeZone.Information

The folloiwing example demonstrates how to retrieve time zone offset using SP.TimeZone.Information and then convert to SharePoint date/time using moment().zone():

var listTitle = 'Calendar';

var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var item = list.getItemById(1);
context.load(item);

var timeZone = web.get_regionalSettings().get_timeZone();
context.load(timeZone);

context.executeQueryAsync(
 function() {
    var eventDate = item.get_item('EventDate');
    var endDate = item.get_item('EndDate');

    var timeZoneOffset = getTimeZoneOffset(timeZone); //exract TimeZone Offset

    var localEventDate = moment(eventDate).zone(timeZoneOffset);
    var localEndDate = moment(endDate).zone(timeZoneOffset);


    console.log("Event Start date: " + localEventDate.format('YYYY-MM-DD hh:mm a'));
    console.log("Event End date: " + localEndDate.format('YYYY-MM-DD hh:mm a'));

  },
  logError
);  

function logError(sender,args)
{
    console.log(args.get_message());
}


function getTimeZoneOffset(timeZone)
{
   return  timeZone.get_information().get_bias() +    timeZone.get_information().get_daylightBias() + timeZone.get_information().get_standardBias();          
}

Using SP.TimeZone.utcToLocalTime

SP.TimeZone.utcToLocalTime Method - converts the specified DateTime value from Coordinated Universal Time (UTC) to local time

The following example demonstrates how to use SP.TimeZone.utcToLocalTime:

var listTitle = 'Calendar';

var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var item = list.getItemById(1);
context.load(item);

var timeZone = web.get_regionalSettings().get_timeZone();
context.load(timeZone);


context.executeQueryAsync(
 function onSucceeded() {
    var eventDate = item.get_item('EventDate');
    var endDate = item.get_item('EndDate');

    var localEventDate = timeZone.utcToLocalTime(eventDate);
    var localEndDate = timeZone.utcToLocalTime(endDate);

    context.executeQueryAsync(
      function() {
          console.log(localEventDate.get_value());   
          console.log(localEndDate.get_value());   
      },
      logError       
    );

 },
 logError
);  

function logError(sender,args)
{
    console.log(args.get_message());
}
0

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.