2

My C# server side code is using listJson, it generates time strings like this:

"CaptureTime":"/Date(1399739515000)/"

How to convert into date format in JavaScript client side?

WangHongjian
  • 383
  • 6
  • 16
  • 1
    You probably don't want to use MS JSON serializers in the first place. http://james.newtonking.com/json is pretty much .NeT de-facto json serializer nowadays. – Stan May 22 '14 at 01:21
  • Consult this article: http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx And consider using JSON.NET, as Steve mentioned. – Lukas S. May 22 '14 at 01:23
  • possible duplicate of [Parsing Date from webservice](http://stackoverflow.com/a/13637479/1048572) – Bergi May 22 '14 at 03:50
  • possible duplicate of [Format a Microsoft JSON date?](http://stackoverflow.com/q/206384/1048572) – Bergi Mar 17 '15 at 11:31

3 Answers3

1

You can do it like this

<script>
d = new Date(1399739515000)
</script>

then d will be a javascript variable that you can then manipulate it on your scripts, like this code for example

d.toUTCString();
fedmich
  • 5,343
  • 3
  • 37
  • 52
1

Thanks to @fedmich:

var s='/Date(1399739515000)/';
var r=/\/Date\((\d*?)\)\//.exec(s);
var d=new Date(parseInt(matches[1]));
console.log(d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate());

http://jsfiddle.net/walkingp/7cA9p/

WangHongjian
  • 383
  • 6
  • 16
0

Real short hand:

Date.prototype.yyyymmdd = function() {
     var yyyy = this.getFullYear().toString();
     var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
     var dd  = this.getDate().toString();
     return yyyy + '-' + (mm[1]?mm:"0"+mm[0]) + '-' + (dd[1]?dd:"0"+dd[0]); // padding
};

var s='/Date(1399739515000)/';
var regex = /(\d+)/g;
alert(new Date(parseInt(s.match(regex))).yyyymmdd());

Reference

Community
  • 1
  • 1
jhyap
  • 3,779
  • 6
  • 27
  • 47