1
Date.parseWeird=Date.prototype.parseWeird=function(d)
{
    return new Date(Date.parse(d));
};

var d = /Date(-65424600000)/

How can i parse this date and show in MM/DD/YY format.

16/09/1956
7
  • 1
    I'm not really understanding the question here. Are you trying to parse the negative number as a Date? Commented Jul 20, 2011 at 13:33
  • What sort of a thing is /Date(-65424600000)/? Commented Jul 20, 2011 at 13:37
  • @John: Many of your questions have a negative score. It may be worth looking at some higher-scored questions to see how it's done! Commented Jul 20, 2011 at 13:38
  • @Tomalak Gerek'kal: Can't help if someone marks a negative score, no one really understands that if someone is not able to explain properly... they give negative stuffs... i am learning... i am editing the question back Commented Jul 20, 2011 at 13:40
  • That is the format from a JSON string from .NET. I'm assuming it is supposed to be a string. Also, the negative number is the time since UNIX Epoch but not going forward from 1970 but backwards from 1970 Commented Jul 20, 2011 at 13:42

2 Answers 2

2

I remove the extra:

Date.parseWeird=Date.prototype.parseWeird=function(d)
{
    return new Date(parseInt(/-?\d+/.exec(d)[0], 10));
};

var x = Date.parseWeird('/Date(-65424600000)/');
alert((x.getMonth()+1) + "/" + x.getDate() + "/" + x.getFullYear());

The express is looking for 0 or 1, ?, negative sign, followed by any number, + , of digits, \d.

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

5 Comments

Thanks... for this code.. can u explain me the regular experssion a little bit and what does exec do
Don't forget x.getMonth() + 1
John I've explained the expression. @Alex, thank you for pointing that out. I always forget that.
I think my regex is easier to understand, no? d = d.replace(/[^\-\d]/g,"");
@mplungjan, perhaps it is. I am not a RegExp expert by any means. I usually just hack something together that works; then forget what all the symbols mean.
2

Instead of fixing it on the client, fix it on the server

http://james.newtonking.com/archive/2009/02/20/good-date-times-with-json-net.aspx

JsonConverters

With no standard for dates in JSON, the number of possible different formats when interoping with other systems is endless. Fortunately Json.NET has a solution to deal with reading and writing custom dates: JsonConverters. A JsonConverter is used to override how a type is serialized.

public class LogEntry
{
  public string Details { get; set; }
  public DateTime LogDate { get; set; }
}

[Test]

public void WriteJsonDates()
{
  LogEntry entry = new LogEntry
  {
    LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
    Details = "Application started."
  };

  string defaultJson = JsonConvert.SerializeObject(entry);

  // {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}

  string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());

  // {"Details":"Application started.","LogDate":new Date(1234656000000)}

  string isoJson = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());

  // {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}

}

If not, does this work for you?

DEMO HERE

Date.parseWeird=Date.prototype.parseWeird=function(d) {
  // remove anything not a digit or - and convert to number
  return new Date(parseInt(d.replace(/[^\-\d]/g,""),10));
};

var d = "\/Date(-65424600000)\/"
var newDate = Date.parseWeird(d);
var mm = newDate.getMonth()+1;
if (mm<10) mm="0"+mm;
var dd = newDate.getDate();
if (dd<10) dd="0"+dd;
document.write(""+mm+"/"+dd+"/"+newDate.getFullYear())

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.