Generate an RFC 3339 timestamp similar to Google Tasks API?

前端 未结 7 2172
时光取名叫无心
时光取名叫无心 2020-12-02 20:45

I am in the process of building an app that syncs with Google Tasks. As part part of the syncing, I want to compare the local task and the API task, and see which one has be

相关标签:
7条回答
  • 2020-12-02 20:52

    It seems like a lot of complicated answers have been given, but this works just fine, does it not?

    (new Date()).toISOString()
    
    0 讨论(0)
  • 2020-12-02 20:52

    If you are using Google Script, another option is to use Utilities.formatDate URL below:

    https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)

    Sample code from above URL:

    // This formats the date as Greenwich Mean Time in the format
    // year-month-dateThour-minute-second.
    var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
    
    0 讨论(0)
  • 2020-12-02 20:55

    The Z behind the first date indicates it's UTC (Zulu) time, without the Z it will use the local (computer) time, which could be several time zones off.

    See: http://en.wikipedia.org/wiki/UTC

    0 讨论(0)
  • 2020-12-02 20:57

    try this:

    Date.prototype.setRFC3339 = function(dString) {
        var utcOffset, offsetSplitChar;
        var offsetMultiplier = 1;
        var dateTime = dString.split("T");
        var date = dateTime[0].split("-");
        var time = dateTime[1].split(":");
        var offsetField = time[time.length - 1];
        var offsetString;
        offsetFieldIdentifier = offsetField.charAt(offsetField.length - 1);
        if (offsetFieldIdentifier == "Z") {
            utcOffset = 0;
            time[time.length - 1] = offsetField.substr(0, offsetField.length - 2);
        } else {
            if (offsetField[offsetField.length - 1].indexOf("+") != -1) {
                offsetSplitChar = "+";
                offsetMultiplier = 1;
            } else {
                offsetSplitChar = "-";
                offsetMultiplier = -1;
            }
            offsetString = offsetField.split(offsetSplitChar);
            time[time.length - 1] == offsetString[0];
            offsetString = offsetString[1].split(":");
            utcOffset = (offsetString[0] * 60) + offsetString[1];
            utcOffset = utcOffset * 60 * 1000;
        }
    
        this.setTime(Date.UTC(date[0], date[1] - 1, date[2], time[0], time[1], time[2]) + (utcOffset * offsetMultiplier));
        return this;
    };
    

    source: http://blog.toppingdesign.com/2009/08/13/fast-rfc-3339-date-processing-in-javascript/

    0 讨论(0)
  • 2020-12-02 21:13

    The formatting is ISO so (new Date()).toISOString() will give you that form. Which as I'm reading might need to be shimmed:

    /* use a function for the exact format desired... */
    function ISODateString(d){
     function pad(n){return n<10 ? '0'+n : n}
     return d.getUTCFullYear()+'-'
          + pad(d.getUTCMonth()+1)+'-'
          + pad(d.getUTCDate())+'T'
          + pad(d.getUTCHours())+':'
          + pad(d.getUTCMinutes())+':'
          + pad(d.getUTCSeconds())+'Z'}
    
    var d = new Date();
    print(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z
    

    Source: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

    0 讨论(0)
  • 2020-12-02 21:13

    I've found the moment.js library nice for working with time in javascript. moment().format() yields a timestamp in the format expected by the Google API for a datetime. Or, to not depend on the default format being correct for your application,

    moment().format("YYYY-MM-DDTHH:mm:ssZ")
    

    All the string options (including fractional seconds if that's what you need): http://momentjs.com/docs/#/displaying/format/

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