1

In my javascript i enter date in below format as string

12.12.2014

I want to convert to JSON date format like below

/Date(1358866800000)/

How could i achieve this. I tried below code which converts to JSON format but doesnt work.

function convertToJSONDate(strDate){
var dt = new Date(strDate);
var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));
return '/Date(' + newDate.getTime() + ')/';
}

When i try to use above function like convertToJSONDate("12.12.2014"), i get date like this '/Date(NaN)/

How could i achieve this?

James
  • 1,827
  • 5
  • 39
  • 69
  • None of [these](http://stackoverflow.com/q/1056728/1169519)? Please check also the documentation to find out the correct form of the argument passed to `Date`. Currently you've invalid form. – Teemu Dec 14 '14 at 15:48
  • @Teemu none of your examples talks about converting to JSON. Can you give me concrete example. – James Dec 14 '14 at 15:53
  • You can convert to JSON using `JSON.stringify()`, but the argument passed to `Date` must be correctly formed. – Teemu Dec 14 '14 at 15:56
  • @Teemu Will really appreciate if you can provide me fiddle showing what exactly to do. – James Dec 14 '14 at 15:58
  • 1
    How about reading [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON) MDN article? Also your "JSON format" looked strange to me, but [no wonder](http://stackoverflow.com/q/10286204/1169519). – Teemu Dec 14 '14 at 16:01

3 Answers3

2

The string you are passing to Date's constructor is not valid

function convertToJSONDate(strDate){
  var splitted = strDate.split(".");
  var dt = new Date(splitted[2],splitted[0],splitted[1]);
  var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));
  return '/Date(' + newDate.getTime() + ')/';
}

convertToJSONDate("12.1.2014");

Another simplified version could be:

function convertToJSONDate(strDate){
  var splitted = strDate.split(".");
  //var dt = new Date(splitted[2],splitted[0],splitted[1]);
  var newDate = new Date(Date.UTC(splitted[2], splitted[0], splitted[1]));
  return '/Date(' + newDate.getTime() + ')/';
}

convertToJSONDate("12.1.2014");
AlexBcn
  • 2,450
  • 2
  • 17
  • 28
0

@AlexBcn Great answer, but you need to subtract 1 from the month because months are zero-based.

function convertToJSONDate(strDate){
        var splitted = strDate.split(".");
        var newDate = new Date(Date.UTC(splitted[2], (splitted[1] - 1), splitted[0]));
        return '/Date(' + newDate.getTime() + ')/';
    }
  //console.log(convertToJSONDate("10.01.2018")); 
  //Output: Wed Jan 10 2018 01:00:00 GMT+0100 (Central European Standard Time) 
  //Output without subtraction: Sat Feb 10 2018 01:00:00 GMT+0100 (Central European Standard Time)
stenak
  • 21
  • 4
-3

try like this..

@JsonSerialize(using=CustomJsonDateSerializer.class) @JsonDeserialize(using=CustomJsonDateDeserializer.class)

  • Please try to improve your answer. That snippet is not enough in order to solve this problem – chomp Nov 05 '20 at 13:08