0

I am storing a UTC date time in a SharePoint list and fetching it's value in c#, converting into milliseconds from 1 Jan 1970 and passing those milliseconds to JavaScript to get date object.

But when I create a date object, its value remains same as UTC date, I want that value to be in users local time zone and reflecting their daylight saving status.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Anna Jhaveri
  • 93
  • 2
  • 9
  • Can you post what code you have so far? – GVashist Jan 09 '15 at 17:28
  • How do you know it remains as UTC date? How are you "passing those milliseconds to JavaScript"? – Heretic Monkey Jan 09 '15 at 17:56
  • `public static long Convert(DateTime from) { DateTime _jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // return System.Convert.ToInt64((from.ToUniversalTime() - _jan1st1970).TotalMilliseconds); LoggingService.LogError("from : " + from.ToString()); long ticks = (long)((from.ToUniversalTime().Ticks - _jan1st1970.Ticks) / 10000); LoggingService.LogError("ticks : " + ticks.ToString()); return ticks; }` – Anna Jhaveri Jan 10 '15 at 14:55
  • Here is my code to return milliseconds to JavaScript – Anna Jhaveri Jan 10 '15 at 14:56
  • And here is my code to convert milliseconds to date var dt = new Date(TestTime); where TestTime is milliseconds – Anna Jhaveri Jan 10 '15 at 14:57

2 Answers2

0

You can use the TimezoneOffset in javascript, check the following code,

var d = new Date()
var n = d.getTimezoneOffset();

In this way you can calculate the time as you want.

Let me know if you need more details :)

Mouhamad Kawas
  • 370
  • 2
  • 12
  • I have no idea as to the intricacies of international time zones. I have tested it and it returned correct results for my time zone. Check the following article http://javascript.about.com/library/bldst.htm – Mouhamad Kawas Jan 10 '15 at 18:23
0

When you create a new date in Javascript i assume you create it on the client side / client machine:

var d = new Date(millis);

The notion that the value remains the same in UTC no matter where you construct the Date object is correct, it's only a matter of how you display the date: in UTC or in the user's local timezone:

You can run this code to see the difference:

var local = date.toDateString() + ' ' + date.toTimeString();
var utc = date.toUTCString();
alert(local);
alert(utc);

Note that the value of millis is the milliseconds passed since 1970-01-01 00:00:00 UTC no matter where you are in this world. Calling new Date().getTime() on 2 opposite sides of the globe should return the same number of milliseconds.

Sandman
  • 2,577
  • 2
  • 21
  • 32
  • alert(local); - Shows the same date and time that is stored in SharePoint list, and it is not considered as UTC instead it is returned as local time, e.g. Mon Jan 12 2015 06:13:50 UTC+053 alert(utc) returns UTC time of the value stored in SharePoint list e.g. Mon, 12 Jan 2015 00:43:50 UTC I need alert(local) to be considered as UTC time in javascript instead of Local time. Any ideas on how to do that? – Anna Jhaveri Jan 12 '15 at 06:11