In my ASP .NET project we don't use any server controlls and codebehind files and work only with JavaScript, Asynch calls (send/recieve events to server with开发者_StackOverflow中文版 XMLHttpRequest) and SQL procedures.
So, i have a date in JavaScript, before i send it to ASP .NET like this: "Fri Aug 5 00:00:00 UTC+0400 2011" and i need to convert this on server or in JavaScript (before i send it) in format: "2011-08-05T00:00:00+04:00".
How is it better could be done?
Thx.
Solution: use JSON.stringify.
I use this straightforward method:
<script type="text/javascript">
// Covert js-date to ISO8601-string.
function DateToISO8601(dt) {
if (dt == null) return null;
var mnth = dt.getUTCMonth() + 1;
if (mnth < 10) mnth = "0" + mnth;
var day = dt.getUTCDate();
if (day < 10) day = "0" + day;
var yr = dt.getUTCFullYear();
var hrs = dt.getUTCHours();
if (hrs < 10) hrs = "0" + hrs;
var min = dt.getUTCMinutes();
if (min < 10) min = "0" + min;
var secs = dt.getUTCSeconds();
if (secs < 10) secs = "0" + secs;
return yr + "-" + mnth + "-" + day + "T" + hrs + ":" + min + ":" + secs + "Z";
}
</script>
JSON will serialize the JavaScript date for you if your ansyc calls are calling methods with a CLR DateTime as a parameter. You do, however, have to do dateTime.ToLocalTime(), as the JSON value will be received in UTC.
For instance, if you're calling a WCF service or a PageMethod with the signature:
public bool DoSomething(DateTime dt)
Being called from the client side as:
PageMethods.DoSomething(dt, onSuccess, onFailure)
Or
var ws = new myNamespace.imyservice();
ws.DoSomething(dt, onSuccess, onFailure);
The time will be received in UTC on the server side, and you'll have to get back to local time:
dt.ToLocalTime()
I believe this is done by .NET auto-generated client service proxies due to the variable nature of client timezone/locale.
use following code
var now = new Date();
now.format("isoDateTime");
精彩评论