50

I have a function in Javascript that receives a C# DateTime from MVC. If the date is null it should return "-", if it's a valid date it should return the formated date.

IMPORTANT: It's not possible to send the date in another format from C#.

Javascript:

function CheckDate(date) {

  if (date == "Mon Jan 01 0001 00:00:00 GMT+0000 (GMT Daylight Time)")
    return "-";
  else {
    var dat = new Date(date);
    return dat.getFullYear() + dat.getMonth() + dat.getDay();
  }

Is there a better way to compare if the date is the C# New DateTime?

And how do I parse and return the date in "yyyy/MM/dd" format?

6
  • 1
    What's the code converting it on the C# side? For example, it could well be culture-specific to wherever the server is.
    – Jon Skeet
    Commented Jul 30, 2013 at 14:40
  • "Duplicate" stackoverflow.com/questions/1056728/…
    – user1193035
    Commented Jul 30, 2013 at 14:43
  • The code on the server side is just CheckDate(@model.Dated) It's just a DateTime C# object.
    – DK ALT
    Commented Jul 30, 2013 at 14:44
  • 1
    http://momentjs.com/
    – user2587132
    Commented Jul 30, 2013 at 14:46
  • what datetime structure came from c#?@DKALT Commented Jul 30, 2013 at 14:47

3 Answers 3

63

Given the output you're stuck with, I can't think of any better way to catch a DateTime of 0 on the javascript side.

Date.parse should work for your parsing needs, but it returns number of milliseconds, so you need to wrap a Date constructor around it:

var date = new Date(Date.parse(myCSharpString));

For the return date, you simply want

date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + (date.getDate() + 1);

(date.getMonth and date.getDate are 0-indexed instead of 1-indexed.)

Fiddle: http://jsfiddle.net/GyC3t/

EDIT Thanks to JoeB's catch, let me do a correction. The date.getMonth() function is 0-indexed, but the date.getDate() function is 1-indexed. The fiddle was "working" with the +1 because date.getMonth works in local time, which is before UTC. I didn't properly check the docs, and just added 1, and it worked with the fiddle.

A more proper way to do this is:

For the return date, you simply want

date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + (date.getUTCDate());

(date.getMonth is 0-indexed while date.getDate is 1-indexed but susceptible to time-zone differences.)

Fiddle: http://jsfiddle.net/GyC3t/25/

5
  • 3
    Javascript date months are 0 indexed, but day are 1 indexed.
    – JoeB
    Commented Mar 7, 2014 at 15:59
  • @JoeB Wow. That was horrible of me. Thanks for catching that! Commented Mar 7, 2014 at 16:30
  • I want to get the month and date in 2 digit, any idea for do it in inside javascript ? Commented Oct 10, 2014 at 5:48
  • @Gupta Your comment is imprecise. I already give 2 digit answers. If you want to know how to zero pad a number like 4 to 04, you really just want to do a Google search for "zero pad javascript". Here's one of the first answers that pops up: stackoverflow.com/questions/10073699/… Commented Oct 10, 2014 at 8:29
  • new Date(Date.parse(myCSharpString)) will produce an identical result to new Date(myCSharpString), the use of Date.parse is entirely redundant.
    – RobG
    Commented Oct 20, 2021 at 9:01
2

I use the following to pass a Javascript Date into C#:

var now = new Date();
var date = (now.getTime() / 86400000) - (now.getTimezoneOffset() / 1440) + 25569;

So if you get the number of milliseconds from C#, it should be something like this:

var csharpmilliseconds;
var now = new Date();
var date = new Date((csharpmilliseconds + (now.getTimezoneOffset() / 1440) - 25569) * 86400000);
1

I made a variant, that I think is a bit more readable

/// <summary>
/// Start of Javascript Date
/// </summary>
static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

/// <param name="ms">From js Date.getTime()</param>
/// <returns>Date in UTC, use ToLocalTime()</returns>
public static DateTime FromJavascriptMs(string ms)
{
    var millisec = double.Parse(ms);
    DateTime result = UnixEpoch.AddMilliseconds(millisec);
    return result;
}

public static double ToJavascriptMs(DateTime date)
{
    TimeSpan duration = date.Subtract(UnixEpoch);
    return duration.TotalMilliseconds;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.