77

I have been reading that if you want to convert from JavaScript dates to C# dates you should use getTime() and then add that result to a C# DateTime.

Suppose I have this JavaScript time:

Date {Tue Jul 12 2011 16:00:00 GMT-0700 (Pacific Daylight Time)}

It renders to 1310522400000 milliseconds

var a = new DateTime(1970, 01, 01).AddMilliseconds(1310522400000);

// result
7/13/2011 2:00:00 AM

So this is wrong. I am not sure what I need to do.

4
  • 1
    They are same... 7/13/2011 2:00:00 AM - 7 (GMT delta) -1 Daylight Savings = Tue Jul 12 2011 16:00:00 GMT-0700 (Pacific Daylight Time) Commented Jul 15, 2011 at 4:34
  • 1
    @Cybernate, I think you're out. There's a 10 hour difference between 16:00 and 2:00. Commented Jul 15, 2011 at 4:43
  • 1
    @Hand: U r right.. I guess time to hit bed.. Commented Jul 15, 2011 at 4:46
  • Possible duplicate: stackoverflow.com/questions/1877788/… Commented Jul 18, 2011 at 4:32

17 Answers 17

97

You could use the toJSON() JavaScript method, it converts a JavaScript DateTime to what C# can recognise as a DateTime.

The JavaScript code looks like this

var date = new Date();
date.toJSON(); // this is the JavaScript date as a c# DateTime

Note: The result will be in UTC time

Sign up to request clarification or add additional context in comments.

5 Comments

its is good solution, but it subtracts time from actual time, means i have norwegian time which is 11:34 it gives me 09:34, can you please explain?
I had to accept this as a "string" on my REST service, then convert it to a DateTime. Otherwise, the REST service method wasn't being called due to an incorrect signature.
@MHanif - I believe if is because the .toJSON() is in UTC time
Yes @Gwasshoppa correct, I've updated the answer to reflect your comment. Thanks!
@MHanif on server side UTC time you can easily convert to local using .ToLocalTime() learn.microsoft.com/en-us/dotnet/api/…
50

First create a string in your required format using the following functions in JavaScript

var date = new Date();
var day = date.getDate();       // yields date
var month = date.getMonth() + 1;    // yields month (add one as '.getMonth()' is zero indexed)
var year = date.getFullYear();  // yields year
var hour = date.getHours();     // yields hours 
var minute = date.getMinutes(); // yields minutes
var second = date.getSeconds(); // yields seconds

// After this construct a string with the above results as below
var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second; 

Pass this string to codebehind function and accept it as a string parameter.Use the DateTime.ParseExact() in codebehind to convert this string to DateTime as follows,

DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Hope this helps...

10 Comments

You should append a "0" in front if javascript returns only a single digit. For that you should use "HH.length()","mm.length()",etc.... and construct the string (var Time)accordingly.
getYear is deprecated. Use .getFullYear
It worked for me but as it has been said before, I used Date.getFullYear().
Use getDate() getDay() returns day of week (index)
Also getMonth() starts from zero in javascript, so you will get 6 for July which will result in wrong month in C#
|
37

You were almost right, there's just need one little fix to be made:

var a = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
    .AddMilliseconds(1310522400000)
    .ToLocalTime();

1 Comment

When I tested this it was off by 2:50
11

If you want to send dates to C# from JS that is actually quite simple - if sending UTC dates is acceptable.

var date = new Date("Tue Jul 12 2011 16:00:00 GMT-0700");
var dateStrToSendToServer = date.toISOString();

... send to C# side ...

var success = DateTimeOffset.TryParse(jsISOStr, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var result);

C# DateTime already understands ISO date formats and will parse it just fine.

To format from C# to JS just use DateTime.UtcNow.ToString("o").

Personally, I'm never comfortable relying on math and logic between different environments to get the milliseconds/ticks to show the EXACT same date and time a user may see on the client (especially where it matters). I would do the same when transferring currency as well (use strings instead to be safe, or separate dollars and cents between two different integers). Sending the date/time as separate values would be just a good (see accepted answer).

Comments

9

DateTime.Parse is a much better bet. JS dates and C# dates do not start from the same root.

Sample:

DateTime dt = DateTime.ParseExact("Tue Jul 12 2011 16:00:00 GMT-0700",
                                  "ddd MMM d yyyy HH:mm:ss tt zzz",
                                  CultureInfo.InvariantCulture);

6 Comments

The problem with this is I don't know how to get that hardcoded string you have from ajax to my MVC action method. If I try to pass in the date object it does not send it. Even if I use like "string" as the parameter in my action method.
You mean you're not able to serialize your JS Date as string to a known format (like Date.toUTCString() and send it to your MVC action? Can you post your ajax code?
Date.toUTCString() returns an ending of GMT (no timezone offset #). The GMTzzzzz part of the formatting causes parsing to fail. Also, you're missing a comma. Finally, it will not work well with all clients with different regional settings. On my system it is "Sun, 04 Feb 2018 09:50:00 GMT" (notice the day BEFORE the month).
@JamesWilkins Thanks for pointing out the typo in the format string. Also, these answers are not meant to be production ready code nor handle every possible permutation there can be. The example code is based on OP's code sample. Localization/Internationalization is a much deeper subject.
I'm basing it on the question "How to convert Javascript datetime to C# datetime?". The OP only showed a Date output, it was not part of their code. The approach to use anything other than the ISO format between JS and C# is a BAD idea for anyone new reading this. ;) Use {Date}.toISOString() and C# will parse it naturally.
|
5

Since I'm in a different timezone, my JavaScript and C# end up having 2 hours difference between the same date (even when I tried to send the date to a webservice as a date [not converted to string/another object]).

I tried to use the getTime() in JavaScript and add the milliseconds to a C# date (starting on 1970-01-01) but I've always ended up with two hours in advance on my C# date.

To grant that I would get the same Date and Hour in both sides I ended up doing this:

In JavaScript I've used the UTC function:

var jsDate = Date.UTC(year,month,day,hours,minutes,seconds,millisec);

And in C# to get the correct DateTime I did this:

var date = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds(jsDate);

Hope it helps someone.

Comments

5

If you are using moment.js in your application.

var x= moment(new Date()).format('DD/MM/YYYY hh:mm:ss')

Pass x to codebehind function and accept it as a string parameter. Use the DateTime.ParseExact() in c# to convert this string to DateTime as follows,

DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

1 Comment

your javascript code should probably be HH:mm:ss since you have HH in backend. If not, 1700 hours becomes 0500 hours.
4

UPDATE: From .NET Version 4.6 use the FromUnixTimeMilliseconds method of the DateTimeOffset structure instead:

DateTimeOffset.FromUnixTimeMilliseconds(1310522400000).DateTime

Comments

1

If you are in the U.S. Pacific time zone, then the epoch for you is 4 p.m. on December 31, 1969. You added the milliseconds since the epoch to

new DateTime(1970, 01, 01)

which, since it did not have a timezone, was interpreted as being in your timezone.

There is nothing really wrong with thinking of instants in time as milliseconds since the epoch but understand the epoch is only 1970-01-01T00:00:00Z.

You can't think of instants in times, when represented as dates, without timezones.

Comments

1
var date = new Date(2022,11,9).toISOString();

This creates the ISO string variant of the date in javascript, and in your .net API, you can expect a DateTime type of parameter, because c# automatically translates iso string dates into DateTime objects.

Honorable mention:

enter image description here

Comments

0

I think you can use the TimeZoneInfo....to convert the datetime....

    static void Main(string[] args)
    {
        long time = 1310522400000;
        DateTime dt_1970 = new DateTime(1970, 1, 1);
        long tricks_1970 = dt_1970.Ticks;
        long time_tricks = tricks_1970 + time * 10000;
        DateTime dt = new DateTime(time_tricks);

        Console.WriteLine(dt.ToShortDateString()); // result : 7/13
        dt = TimeZoneInfo.ConvertTimeToUtc(dt);

        Console.WriteLine(dt.ToShortDateString());  // result : 7/12
        Console.Read();
    }

Comments

0

JavaScript (HTML5)

function TimeHelper_GetDateAndFormat() {
    var date = new Date();

    return MakeValid(date.getDate()).concat(
        HtmlConstants_FRONT_SLASH,
        MakeValid(date.getMonth() + 1),
        HtmlConstants_FRONT_SLASH,
        MakeValid(date.getFullYear()),
        HtmlConstants_SPACE,
        MakeValid(date.getHours()),
        HtmlConstants_COLON,
        MakeValid(date.getMinutes()),
        HtmlConstants_COLON,
        MakeValid(date.getSeconds()));
}

function MakeValid(timeRegion) {
    return timeRegion !== undefined && timeRegion < 10 ? ("0" + timeRegion).toString() : timeRegion.toString();
}

C#

private const string DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";

public DateTime? JavaScriptDateParse(string dateString)
{
    DateTime date;
    return DateTime.TryParseExact(dateString, DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out date) ? date : null;
}

Comments

0

There were some mistakes in harun's answer which are corrected below:

1) date where harun used getDay() which is incorrect should be getDate()

2) getMonth() gives one less month than actual month, So we should increment it by 1 as shown below

var date = new Date();
var day = date.getDate();           // yields 
var month = date.getMonth() + 1;    // yields month
var year = date.getFullYear();      // yields year
var hour = date.getHours();         // yields hours 
var minute = date.getMinutes();     // yields minutes
var second = date.getSeconds();     // yields seconds

// After this construct a string with the above results as below
var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second; 

Pass this string to codebehind function and accept it as a string parameter.Use the DateTime.ParseExact() in codebehind to convert this string to DateTime as follows,

DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

This Worked for me! Hope this help you too.

Comments

0

JS:

 function createDateObj(date) {
            var day = date.getDate();           // yields 
            var month = date.getMonth();    // yields month
            var year = date.getFullYear();      // yields year
            var hour = date.getHours();         // yields hours 
            var minute = date.getMinutes();     // yields minutes
            var second = date.getSeconds();     // yields seconds
            var millisec = date.getMilliseconds();
            var jsDate = Date.UTC(year, month, day, hour, minute, second, millisec);
            return jsDate;
        }

JS:

var oRequirementEval = new Object();
var date = new Date($("#dueDate").val());

CS:

requirementEvaluations.DeadLine = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
    .AddMilliseconds(Convert.ToDouble( arrayUpdateRequirementEvaluationData["DeadLine"]))
    .ToLocalTime();

Comments

0

You can also send Js time to C# with Moment.js Library :

JavaScript : var dateString = moment(new Date()).format('LLLL')

C# : DateTime.Parse(dateString);

1 Comment

'LLLL' format doesn't work for me.. it works with ("MM/DD/YYYY HH:mm:ss")
0
var date = new Date(this.newItemDate).toDateString();

this line of code works for me

Comments

-3
Newtonsoft.Json.JsonConvert.SerializeObject(Convert.ToDateTime(dr[col.ColumnName])).Replace('"', ' ').Trim();

Comments

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.