2

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();
caml.set_viewXml(...);

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

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'));
4
  • I think you will find what you are looking for here: sharepoint.stackexchange.com/questions/25874/…
    – Ali Jafer
    Commented Aug 13, 2012 at 15:45
  • It's another question. My question is how to convert DateTime that I get form SP.CamlQuery() to current time. Commented Aug 13, 2012 at 16:46
  • in which form u get date? and in which form u required? Commented Aug 14, 2012 at 11:09
  • V_B, I update post Commented Aug 14, 2012 at 11:24

1 Answer 1

2

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).

3
  • "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 Commented Aug 14, 2012 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? Commented Aug 14, 2012 at 13:32
  • "maybe the best way is that you showed (by calculating the offset spweb + local)" - need the same but without server insert Commented Aug 29, 2012 at 11:59

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.