jquery - How to convert normal date to JSON date in JavaScript? - Stack Overflow

My question is easy.There is a lot of information about converting JSON date (Date00213912321) to nor

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()) outputs 2014-03-07T16:29:39.498Z – A1rPun Commented Mar 7, 2014 at 16:29
Add a ment  | 

3 Answers 3

Reset to default 4

If 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信