Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

1 Answer

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.28*Z* (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).

share|improve this answer
    
"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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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