0

Im writing a mvc4 application that will be used globally. Part of the application is recording when a transaction was added or modified.

So I am saving the transaction datetime as UTC. from the clientside whats my best way to display the date as they are expecting?

Is this a javascript function I should be using or should I be doing something within the view?

Diver Dan
  • 9,953
  • 22
  • 95
  • 166

2 Answers2

0

It depends on your preference. If you have user time zone on the server side, stored in the database, the simplest way is to use DateTimeOffset.

Example: Creating a DateTime in a specific Time Zone in c# fx 3.5.

Alternatively, use JavaScript getTimezoneOffset() Method, set cookie and read it on server. Also you can use many client libs, especially if you use client templating.

Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
0

Internally, javascript date objects store the time as a number of milliseconds since an epoch UTC (1970-01-01T00:00:00Z). The simplest way to transfer time is using that number, so:

// 2012-09-16T10:30:00.000Z
var ms = 1347791400000;

// Create a new date object using UTC milliseconds
var d = new Date(ms);

alert(d); // Shows local time equivalent, e.g. 2012-09-16T20:30:00GMT+1000

Alternatively, you can pass a UTC timestamp, split it into its components and convert it to a date object using Date.UTC. Note that months are zero based so one must be subtracted from the calendar month number.

To get the UTC millisecond value for a local time, use Date.prototype.getTime. For the example above:

alert(d.getTime()); // 1347791400000
RobG
  • 142,382
  • 31
  • 172
  • 209