JavaScript SP.CamlQuery() return DateTime fields with offset (in my case +11 hours). In standart Display/Edit forms and views display correct DateTime.

How to get correct DateTime via SP.CamlQuery()?

UPD: JS to get list items

var caml = new SP.CamlQuery();

var bookingsItems = this._bookingsList.getItems(caml);

this._context.executeQueryAsync(function (sender, args) {
    var enumerator = bookingsItems.getEnumerator();

    var items = [];
    while (enumerator.moveNext()) {
        var listItem = enumerator.get_current();
        var bookingStart = listItem.get_item('BookingStart') // DateTime 
}, this._QuerylogProcess);

For example bookingStart = 2012-8-14 12:00. If open DispForm.aspx for this listItem bookingStart = 2012-8-14 1:00

UPD: Solution with server insert: C# code to calculate web reginal settings offset:

private TimeSpan _GetUtcOffset(SPWeb web)
    DateTime utcDate = DateTime.UtcNow;
    DateTime localDate = web.RegionalSettings.TimeZone.UTCToLocalTime(utcDate);
    return localDate - utcDate;

JavaScript function to calculate correct dateTime

var dateTimeCalculateConsideringWebRegionalSettings = function(date){
    var utcOffsetMinutes = <%= Convert.ToInt32(UtcOffset.TotalMinutes) %> + new Date().getTimezoneOffset();

    var newDate = new Date(date.getTime());
    newDate.setTime(newDate.getTime() + (utcOffsetMinutes*60*1000));
    return newDate;

Use js function:

var bookingStart = dateTimeCalculateConsideringWebRegionalSettings(listItem.get_item('BookingStart'));

Very interesting question.

When you retrieve the datetime field with SharePoint javascript API, you get date object which has everything you need, the date and time and offset. Work with this as with an object.

Handling datetime in sharepoint javascript api

Here is an example where I get a listitem and a datetime field ("Modified" in my case) I have two hours offset: 11.28 gmt+2h, when I create a iso string it becomes 09.28Z (zero time)

By the way, your question has a little to do with CamlQuery. No need to use CamlQuery in the title.

EDIT: If you want to show the same datetime as it is defined in web regional settings, maybe the best way is that you showed (by calculating the offset spweb + local).

  • "I have two hours offset" -The offset you talk about is browser local settings offset. But there is another offset came from web regionsal settings – user1167761 Aug 14 '12 at 12:52
  • Okay, I see. You get the "local" time, but you want to get the the as it is defined in web regional settings? – Anatoly Mironov Aug 14 '12 at 13:32
  • "maybe the best way is that you showed (by calculating the offset spweb + local)" - need the same but without server insert – user1167761 Aug 29 '12 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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