What is the difference between toGMTstring() and toUTCstring()?

前端 未结 4 1288
醉梦人生
醉梦人生 2021-02-14 06:53

I am saving data in MongoDB server from Node.js application (using Mongoose).

Consider following code:

var mongoose = require(\'mongoose\');
var Schema =         


        
相关标签:
4条回答
  • 2021-02-14 07:12

    for ISO use toISOString()

    var today = new Date();
    var ISOstring = today.toISOString();
    // 2020-08-03T23:59:58.123Z
    
    0 讨论(0)
  • 2021-02-14 07:28

    GMT and UTC are different timezones, they are Greenwich Mean Time and Coordinated Universal Time respectively. GMT is a 'solar' timezone, whereas UTC is 'atomic'. For most purposes they are essentially the same thing, however UTC is more 'universal'.

    Interestingly the documentation you point to for toUTCString still show a GMT output:

    var today = new Date();
    var UTCstring = today.toUTCString();
    // Mon, 03 Jul 2006 21:44:38 GMT
    

    For interchange of data between application I would prefer to use something like ISO8601, which uses the 'Z' suffix for UTC:

    2013-01-16T08:19Z
    

    Where the 'Z' confusingly stands for 'Zulu time'!

    0 讨论(0)
  • 2021-02-14 07:34

    From what I can see they are the same. And the documentation at MDN already states that toGMTString has been deprecated in favor of toUTCString:

    toGMTString() is deprecated and should no longer be used. It remains implemented only for backward compatibility; please use toUTCString() instead.

    0 讨论(0)
  • 2021-02-14 07:36

    Mostly use for formatting date and time (Human Readable). You can also use toLocaleDateString()

    var event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
    
    var options = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' };
    
    console.log(event.toLocaleDateString('en-US', options));
    
    0 讨论(0)
提交回复
热议问题