Silviu Preda
Silviu Preda

Reputation: 628

Convert datetime object into specific string Javascript

I have a date in the format dd.MM.yyyy HH:mm, for example 31.01.2014 11:24. How can I obtain, in javascript, the string "/Date(1391160281569)/"?

Upvotes: 0

Views: 155

Answers (2)

Praveen
Praveen

Reputation: 56539

Here is an approach

var date = "31.01.2014 11:24";
var sp1 = date.split(/[:. ]/);
var newDate = new Date(sp1[2], (+sp1[1] - 1), sp1[0], sp1[3], sp1[4]);
var milliSeconds = newDate.getTime();
var urFormat = "/Date(" + milliSeconds + ")/";
alert(urFormat)

JSFiddle

Upvotes: 1

Silviu Preda
Silviu Preda

Reputation: 628

I took me a while but i got this:

var theStringVersion = "/Date("+$.now()+")/";

Of course, for a real date, i would have to get the timestamp for it.

Upvotes: 0

Related Questions