2

Possible Duplicate:
Converting .NET DateTime to JSON

How can I convert a date value in "mm/dd/yyyy hh:mm:ss" format to "/Date(1324414956395)/" format (Json Date).

I am passing the date format "mm/dd/yyyy hh:mm:ss" into a MVC controller action method and I need to compare that to another date in JsonDate format in the code. Thanks for help.

Community
  • 1
  • 1
ZVenue
  • 4,967
  • 16
  • 61
  • 92
  • It isn't JSONDate, it is a timestamp ! – Bakudan Dec 28 '11 at 16:24
  • 5
    See the answer by Jeff Meatball Yang: http://stackoverflow.com/questions/1016847/converting-net-datetime-to-json – keyboardP Dec 28 '11 at 16:25
  • @Bakudan-ханювиги okay.. I am storing the time stamp in Json document in that case. Do you know how I can convert mm/dd/yyyy into that timestamp format? – ZVenue Dec 28 '11 at 16:27
  • @keyboardP That link talks about converting TimeStamp "/Date(1324414956395)/" into mm/dd/yyyy .. I want the other way round.Please read original post. Thank you – ZVenue Dec 28 '11 at 16:28
  • @ZVenue - Jeff Meatball Yang's method takes in a DateTime object (as an extension method) and converts it to a double. It is not converting from a double to mm/dd/yyyy. – keyboardP Dec 28 '11 at 16:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6196/discussion-between-zvenue-and-keyboardp) – ZVenue Dec 28 '11 at 16:48

2 Answers2

4

dt.ToUniversalTime() won't be recognised when using DateTime?. DateTime? is essentially Nullable<DateTime>. What you need to do is use the Value property to retrieve the DateTime object

 dt.Value.ToUniversalTime();

You can then use the code from this post (Jeff Meatball Yang's answer, not the accepted answer) with your nullable DateTime.

Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
0

(DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;

Or

This superior solution from this link: Converting .NET DateTime to JSON

DateTime d1 = new DateTime(1970, 1, 1);
    DateTime d2 = dt.ToUniversalTime();
    TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
    return ts.TotalMilliseconds;

It seems that this is a bad way to compare dates, however. This is basically counting the ticks since 1970 (or something) and is going to be a pretty long and accurate number. Even if you needed to make certain that the dates matched down to a second it seems like it would be easier to convert all the universal time to mm/dd/yyyy format and then compare at that point.

Community
  • 1
  • 1
Chase
  • 564
  • 7
  • 22
  • But JavaScript timestamps have an epoch of 01/01/1970. This won't work unless you subtract that offset from `DateTime.Now`. – Jim Mischel Dec 28 '11 at 16:32
  • @Chase: How do I do this for a specific date .. not for "Now" – ZVenue Dec 28 '11 at 16:32
  • @JimMischel - Right you are. You can offset it with the actual 1970 value or the solution that I linked from the other person which is much more readable. – Chase Dec 28 '11 at 16:38
  • @Chase I cannot get dt.ToUniversalTime() recognized.. I have "using system" namespace. – ZVenue Dec 28 '11 at 16:42