1

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 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.

1

3 Answers 3

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I've just found, that date converts using JSON.stringify, but thx.
@FSou1 you should consider posting your own answer and then accepting it.
see my answer. that is easier than this.
1

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.

Comments

1

use following code

var now = new Date();
now.format("isoDateTime");

1 Comment

this is the most easiest way of doing this. more information..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.