Generate an RFC 3339 timestamp similar to Google Tasks API?

旧城冷巷雨未停 提交于 2019-11-27 19:22:14

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

Trenton D. Adams

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

(new Date()).toISOString()

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/

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'");

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/

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

It looks more pretty: new Date().toISOString().split('.')[0] + 'Z'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!