简体   繁体   中英

Convert datetime from C# to string in javascript

I have c# code that returns something like

"2013-11-05T16:55:34.7567325-05:00" as the date to the javascript code.

How do I display this like this "11/05/2013" ?

There is only one date string format that is supported by ECMAScript and that is a version of ISO 8601. However, not all browsers in use support it and most don't support different timezones. Otherwise, date string parsing is implementation dependent (ie varies from browser to browser).

There is (non–standard) support for various string formats, but they aren't universally supported.

So your best bet is to manually parse the string.

// parse an ISO 8601 date string with time zone
// e.g. "2013-11-05T16:55:34.7567325-05:00"  -> Wed Nov 06 2013 07:55:34 GMT+1000
// Any missing date value is treated as "01", any missing time value is 0.
// A missing timezone is treated as "Z" (UTC).
// Civil timezone abbreviations (e.g. EST, CET) are not supported
function isoStringToDate(s) {

  // Split into parts
  var p = s.split(/\D/);

  // Calculate offset as minutes to add using sign
  // A missing timezone is treated as UTC
  var offsetSign = /-\d\d:\d\d$/.test(s)? 1 : -1;
  var offset = offsetSign * ((p[7] || 0) * 60 + +(p[8] || 0));

  // Get milliseconds - pad if required. Values beyond 3 places are
  // truncated by the Date constructor so will not be preserved in the
  // resulting date object (i.e. it's a limitation of Date instances)
  p[6] = (p[6]? p[6] : '0') + '00'; 
  var ms = p[6].substring(0,3) + '.' + p[6].substring(3);

  // Create local date as if values are UTC
  var d = new Date(Date.UTC(p[0], p[1]? --p[1] : 0, p[2] || 1, 
                            p[3] || 0, p[4] || 0, p[5] || 0, ms)); 

  // Adjust for timezone in string
  d.setMinutes(d.getMinutes() + offset);
  return d;
}

// Given a Date object, return a string in the US format mm/dd/yyyy
function formatAsUSDateString(d) {
  function z(n){return (n<10?'0':'') + n;}
  return z(d.getMonth() + 1) + '/' + z(d.getDate()) + '/' + d.getFullYear();
}

var d = isoStringToDate('2013-11-05T16:55:34.7567325-05:00');

console.log(formatAsUSDateString(d)); // 11/06/2013 for me but I'm UTC+10

Timezone allows for "Z" or "+/-HH:mm" (anything other than +/-HH:mm is treated as Z). Supports all ISO 8601 string formats specified by ES5 where missing date parts are treated as "1" and missing time parts are treated as "0", eg "2013-11" -> "2013-11-01T00:00:00Z".

this date string can be converted to javascript datetime object, see the code to get what you want:

Option A: via javascript

var yourDateString = '2013-11-05T16:55:34.7567325-05:00'; //date that you receive from c#
var yourDate = new Date(yourDateString);
var yourFormatedDate = yourDate.getDay() + "/" + yourDate.getMonth() + "/" + yourDate.getFullYear();

alert(yourFormatedDate);

Option B: via C#

code behind:

String formateDate = String.Format("{0:MM/dd/yyyy}", yourDateVar);

directly to javascript in ASP.NET:

var javascriptVar = '<%=String.Format("{0:MM/dd/yyyy}", yourDateVar); %>';

The easiest way is with moment.js .

moment("2013-11-05T16:55:34.7567325-05:00").format("MM/DD/YYYY")

That will assume you want the date for the exact moment of this time, in the viewer's local time zone. It's possible this could fall on a different date for that user due to time zone variations.

If you want to preserve the exact values you brought in, then instead use this:

moment.parseZone("2013-11-05T16:55:34.7567325-05:00").format("MM/DD/YYYY")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM