How do you convert a JavaScript date to UTC?

后端 未结 29 2780
无人共我
无人共我 2020-11-22 00:50

Suppose a user of your website enters a date range.

2009-1-1 to 2009-1-3

You need to send this date to a server for some processing, but th

29条回答
  •  庸人自扰
    2020-11-22 00:59

    This method will give you : 2017-08-04T11:15:00.000+04:30 and you can ignore zone variable to simply get 2017-08-04T11:15:00.000.

    function getLocalIsoDateTime(dtString) {
        if(dtString == "")
            return "";
        var offset = new Date().getTimezoneOffset();
        var localISOTime = (new Date(new Date(dtString) - offset * 60000 /*offset in milliseconds*/)).toISOString().slice(0,-1);
        //Next two lines can be removed if zone isn't needed.
        var absO = Math.abs(offset);
        var zone = (offset < 0 ? "+" : "-") + ("00" + Math.floor(absO / 60)).slice(-2) + ":" + ("00" + (absO % 60)).slice(-2);
        return localISOTime + zone;
    }
    

提交回复
热议问题