DateTime - javascript date

From another answer in Stackoverflow, this conversion from Javascript date to .net DateTime:

long msSinceEpoch = 1260402952906; // Value from Date.getTime() in JavaScript return new DateTime(1970, 1, 1) + new TimeSpan(msSinceEpoch * 10000); 

But how to do the opposite? DateTime to Javascript Date?

Thank,

Aj

+69
javascript c #
Mar 08 '10 at 19:50
source share
9 answers

Try:

 return DateTime.Now.Subtract(new DateTime(1970, 1,1)).TotalMilliseconds 

Edit: true UTC is better, but then we need to be consistent

 return DateTime.UtcNow .Subtract(new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc)) .TotalMilliseconds; 

Although, secondly, it does not matter if both dates are in the same time zone.

+90
Mar 08 '10 at 19:55
source share
β€” -

The JavaScript Date constructor takes the number of milliseconds since Unix (January 1, 1970 00:00:00 UTC). Heres C # that converts a .Net DateTime object to a JavaScript date:

 public static class DateTimeJavaScript { private static readonly long DatetimeMinTimeTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks; public static long ToJavaScriptMilliseconds(this DateTime dt) { return (long)((dt.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 10000); } } 

Using JavaScript:

 var dt = new Date(<%= DateTime.Today.ToJavaScriptMilliseconds() %>); alert(dt); 
+36
Nov 14 '12 at 10:01
source share

With Moment.js just use:

 var jsDate = moment(netDateTime).toDate(); 

Where netDateTime is your DateTime variable, serialized, something like "/Date(1456956000000+0200)/" .

+12
Mar 03 '16 at 12:03
source share

This should do the trick:

 date.Subtract(new DateTime(1970, 1,1)).TotalMilliseconds 
+8
Mar 08 '10 at 19:56
source share

You can try this in your action:

 return DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"); 

And this is in your Ajax success:

 success: function (resultDateString) { var date = new Date(resultDateString); } 

Or is it in your view: (Javascript plus C #)

 var date = new Date('@DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")'); 
+8
Feb 16 '16 at 1:30
source share

I know this is a bit late, but here is the solution I had to come up with for processing dates when you want to be time independent. In fact, this is due to the conversion of everything to UTC.

From Javascript to server :

Send dates as epoch values ​​with timezone offset removed.

 var d = new Date(2015,0,1) // Jan 1, 2015 // Ajax Request to server ... $.ajax({ url: '/target', params: { date: d.getTime() - (d.getTimezoneOffset() * 60 * 1000) } }); 

The server then receives 1420070400000 as the date.

On the server side, convert this epoch value to a datetime object:

 DateTime d = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(epoch); 

At the moment, the date is only the date / time provided by the user as they are provided. Effectively this is UTC.

Transition in another way :

When a server retrieves data from a database, presumably in UTC format, it gets the difference as an epoch (make sure that both date objects are local or UTC):

 long ms = (long)utcDate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; 

or

 long ms = (long)localDate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local)).TotalMilliseconds; 

When javascript gets this value, create a new date object. However, this date object will be assumed in local time, so you need to cancel it in the current time zone:

 var epochValue = 1420070400000 // value pulled from server. var utcDateVal = new Date(epochValue); var actualDate = new Date(utcDateVal.getTime() + (utcDateVal.getTimezoneOffset() * 60 * 1000)) console.log(utcDateVal); // Wed Dec 31 2014 19:00:00 GMT-0500 (Eastern Standard Time) console.log(actualDate); // Thu Jan 01 2015 00:00:00 GMT-0500 (Eastern Standard Time) 

As far as I know, this should work in any time zone where you need to display dates that are not time zone dependent.

+1
Jun 02 '15 at 13:13
source share

If you use MVC with a razor

----- Razor / C #

 var dt1 = DateTime.Now.AddDays(14).Date; var dt2 = DateTime.Now.AddDays(18).Date; var lstDateTime = new List<DateTime>(); lstDateTime.Add(dt1); lstDateTime.Add(dt2); 

--- Javascript

 $(function() { var arr = []; //javascript array @foreach (var item in lstDateTime) { @:arr1.push(new Date(@item.Year, @(item.Month - 1), @item.Day)); } 
  • 1: create a list in C # and populate it
  • 2: create an array in javascript
  • 3: use a razor to iterate over the list.
  • 4: Use @: to return to js and @ to switch to C #
  • 5: -1 per month to fix the month number in js.

Good luck.

+1
Apr 7 '17 at 13:15
source share
 <input type="hidden" id="CDate" value="<%=DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%>" /> 

To convert date to JS date (all numbers):

 var JSDate = $("#CDate").val(); JSDate = Date.parse(JSDate); 
0
Mar 16 '15 at 14:39
source share

This method works for me:

  public sCdateToJsDate(cSDate: any): Date { // cSDate is '2017-01-24T14:14:55.807' var datestr = cSDate.toString(); var dateAr = datestr.split('-'); var year = parseInt(dateAr[0]); var month = parseInt(dateAr[1])-1; var day = parseInt(dateAr[2].substring(0, dateAr[2].indexOf("T"))); var timestring = dateAr[2].substring(dateAr[2].indexOf("T") + 1); var timeAr = timestring.split(":"); var hour = parseInt(timeAr[0]); var min = parseInt(timeAr[1]); var sek = parseInt(timeAr[2]); var date = new Date(year, month, day, hour, min, sek, 0); return date; } 
0
Jan 31 '17 at 12:35
source share



All Articles