Javascript 生成类似于 Google Tasks API 的 RFC 3339 时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7244246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate an RFC 3339 timestamp similar to Google Tasks API?
提问by levi
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 been changed more recently.
我正在构建一个与 Google Tasks 同步的应用程序。作为同步的一部分,我想比较本地任务和 API 任务,看看最近更改了哪个。
Each task from Google's API contains an updated
property, which looks like this:
Google API 中的每个任务都包含一个updated
属性,如下所示:
2011-08-30T13:22:53.108Z
Now I would like to generate a timestamp similar to that, so that every time I update a task on my app it sets a new updated
value. To generate the RFC 3339 timestamp I am using - http://cbas.pandion.im/2009/10/generating-rfc-3339-timestamps-in.htmlwhich generates something like this:
现在我想生成一个类似的时间戳,这样每次我更新我的应用程序上的任务时,它都会设置一个新updated
值。要生成我正在使用的 RFC 3339 时间戳 - http://cbas.pandion.im/2009/10/generating-rfc-3339-timestamps-in.html生成如下内容:
2011-08-30T09:30:16.768-04:00
The issue is, the API date is always coming back as "greater" than the local date, even when the local date is newer. I'm guessing it has something to do with the different formatting between the two.
问题是,API 日期总是比本地日期“大”,即使本地日期更新。我猜这与两者之间的不同格式有关。
Here are two dates, the top is from the Google Tasks API (from about 10 minutes ago), and the bottom one was generated locally a minute ago. When compared which is greater, it's telling me the top one is.
这里有两个日期,最上面的是来自 Google Tasks API(大约 10 分钟前),最下面的是一分钟前在本地生成的。当比较哪个更大时,它告诉我最重要的是。
2011-08-30T13:22:53.108Z
2011-08-30T09:41:00.735-04:00
Is my formatting wrong? What I am doing wrong here? Any help on this is really appreciated.
我的格式不对吗?我在这里做错了什么?对此的任何帮助都非常感谢。
回答by 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()
回答by Joe
The formatting is ISO so (new Date()).toISOString()
will give you that form. Which as I'm reading might need to be shimmed:
格式是 ISO,所以(new Date()).toISOString()
会给你那种形式。我正在阅读的内容可能需要修正:
/* 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
来源:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
回答by Donny Winston
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.js 库非常适合在 javascript 中处理时间。moment().format()
以 Google API 预期的格式为datetime
. 或者,为了不依赖于您的应用程序正确的默认格式,
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/
所有字符串选项(如果您需要,还包括小数秒):http: //momentjs.com/docs/#/displaying/format/
回答by Kilimanjaro T Robbs
If you are using Google Script, another option is to use Utilities.formatDate URL below:
如果您使用的是 Google Script,另一种选择是使用下面的 Utilities.formatDate URL:
Sample code from above URL:
来自上述 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'");
回答by Marek Sebera
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/
来源:http: //blog.toppingdesign.com/2009/08/13/fast-rfc-3339-date-processing-in-javascript/
回答by Aidamina
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.
第一个日期后面的 Z 表示它是 UTC (Zulu) 时间,如果没有 Z,它将使用本地(计算机)时间,这可能是几个时区关闭的。
回答by Morbo
It looks more pretty:
new Date().toISOString().split('.')[0] + 'Z'
看起来更漂亮:
new Date().toISOString().split('.')[0] + 'Z'