My question is easy.
There is a lot of information about converting JSON date (Date/00213912321/) to normal date (7/03/14 10:00 am).
But I need to do the inverse. I have a normal date and I need it in JSON format. How can I achieve that?
I'm using jQuery so if there is some plugin that I can use, it'll be great.
My question is easy.
There is a lot of information about converting JSON date (Date/00213912321/) to normal date (7/03/14 10:00 am).
But I need to do the inverse. I have a normal date and I need it in JSON format. How can I achieve that?
I'm using jQuery so if there is some plugin that I can use, it'll be great.
Share Improve this question edited Oct 20, 2017 at 16:16 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Mar 7, 2014 at 16:11 GalloPintoGalloPinto 6772 gold badges11 silver badges25 bronze badges 2- 1 There's no such thing as a 'JSON date'. I'm not sure what you're trying to achieve here. – Rory McCrossan Commented Mar 7, 2014 at 16:13
-
1
JSON.stringify(new Date())
outputs2014-03-07T16:29:39.498Z
– A1rPun Commented Mar 7, 2014 at 16:29
3 Answers
Reset to default 4If you have a regular date, perhaps the Date object can parse it.
var dateObj = new Date('7/03/14 10:00 am');
timestamp = dateObj.getTime(); // timestamp is 1404396000000
There is no such thing as a JSON Date. JSON doesn't support any type for dates.
00213912321
appears to be epoch time. You can get that from a Date object with the getTime
method.
Therefore:
var date = "Date/" + someDateObject.getTime() + "/";
// convert json date into normal date.
function ConvertJsonDateString(jsonDate)
{
var shortDate = null;
if (jsonDate)
{
var regex = /-?\d+/;
var matches = regex.exec(jsonDate);
var dt = new Date(parseInt(matches[0]));
var month = dt.getMonth() + 1;
const monthNames = ["", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var monthString = monthNames[month];
// var monthString = month > 9 ? month: '0' + month;
var day = dt.getDate();
var dayString = day > 9 ? day : '0' + day;
var year = dt.getFullYear();
shortDate = monthString + '-' + dayString + '-' + year;
}
return shortDate;
};
Ajax Json
{ "data": function (r) {
var date = ConvertJsonDateString(r.Jsondate);
var Expirydt = date == null ? "" : date;
return Expirydt;
}
},
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744963951a4603560.html
评论列表(0条)