How do you convert a JavaScript date to UTC?

后端 未结 29 2036
无人共我
无人共我 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 01:18
    date = '2012-07-28'; stringdate = new Date(date).toISOString();
    

    ought to work in most newer browsers. it returns 2012-07-28T00:00:00.000Z on Firefox 6.0

    0 讨论(0)
  • 2020-11-22 01:18

    If you are dealing with dates a lot, it's worth using moment.js (http://momentjs.com). The method to convert to UTC would be:

    moment(yourTime).utc()
    

    You can use format to change your date to any format you want:

    moment(yourTime).utc().format("YYYY-MM-DD")
    

    There is offset options in moment as well but there is an additional complementary library for dealing with timezone (http://momentjs.com/timezone/). The time conversion would be as simple as this:

    moment.tz(yourUTCTime, "America/New_York")
    
    0 讨论(0)
  • 2020-11-22 01:19

    If you need Date Object

    Passing only date string Date assumes time to be 00:00 shifted by time zone:

    new Date('2019-03-11')
    Sun Mar 10 2019 18:00:00 GMT-0600 (Central Standard Time)
    

    If you add current hours and minutes you get proper date:

    new Date('2019-03-11 ' + new Date().getHours() + ':' + new Date().getMinutes())
    Mon Mar 11 2019 04:36:00 GMT-0600 (Central Standard Time)
    
    0 讨论(0)
  • 2020-11-22 01:20

    Looking at your question its clear that you just want to send the date range to your backend for further post processing.

    I am assuming you are conforming to the standard data guidelines which expect the data to be in a particular format. For example, I use ODATA which is a RESTfull API which expects date time objects to be in the format:-

    YYYY-MM-DDT00:00:00.

    That can be easily achieved via the snippet posted below(Please change the format as per your requirement).

    var mydate;//assuming this is my date object which I want to expose var UTCDateStr = mydate.getUTCFullYear() + "-" + mydate.getUTCMonth() + "-" + mydate.getUTCDate() + "T00:00:00";

    If on the other hand, you are in my situation wherein you have received a date from your backend, and the browser converts that to your local date. You on the other hand are interested in the UTC date then you can perform the following:-

    var mydate;//assuming this is my date object which I want to expose var UTCDate = new Date(mydate);/*create a copy of your date object. Only needed if you for some reason need the original local date*/ UTCDate.setTime(UTCDate.getTime() + UTCDate.getTimezoneOffset() * 60 * 1000);

    The code snippet above basically adds/subtracts the time added/subtracted by the browser based on the timezone.

    For example if I am in EST(GMT-5) and my Service returns a date time object = Wed Aug 17 2016 00:00:00 GMT-0500 my browser automatically subtracts the timezone offset(5hrs) to get my local time. So if I try to fetch the time I get Wed Aug 16 2016 19:00:00 GMT-0500. This causes a lot of problems. There are a lot of libraries out there which will definitely make this easier but I wanted to share the pure JS approach.

    For more info please have a look at: http://praveenlobo.com/blog/how-to-convert-javascript-local-date-to-utc-and-utc-to-local-date/ where in I got my inspiration.

    Hope this helps!

    0 讨论(0)
  • 2020-11-22 01:20

    const event = new Date();

    console.log(event.toUTCString());

    0 讨论(0)
提交回复
热议问题