生成类似于 Google Tasks API 的 RFC 3339 时间戳?
我正在构建一个与 Google 任务同步的应用程序。作为同步的一部分,我想比较本地任务和 API 任务,看看哪一个任务最近发生了更改。
Google API 中的每个任务都包含一个 updated
属性,如下所示:
2011-08-30T13:22:53.108Z
现在我想生成一个与此类似的时间戳,以便每次更新应用程序上的任务时,它都会设置一个新的 <代码>更新值。为了生成我正在使用的 RFC 3339 时间戳 - http:// /cbas.pandion.im/2009/10/generating-rfc-3339-timestamps-in.html 生成如下内容:
2011-08-30T09:30:16.768-04:00
问题也就是说,API 日期总是比本地日期“更大”,即使本地日期较新也是如此。我猜这与两者之间的不同格式有关。
这里有两个日期,顶部来自 Google Tasks API(大约 10 分钟前),底部一个是一分钟前本地生成的。当比较哪个更大时,它告诉我最大的是。
2011-08-30T13:22:53.108Z
2011-08-30T09:41:00.735-04:00
是我的格式错误吗?我在这里做错了什么?非常感谢对此的任何帮助。
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.
Each task from Google's API contains an updated
property, which looks like this:
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.html which generates something like this:
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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
似乎已经给出了很多复杂的答案,但这很好用,不是吗?
It seems like a lot of complicated answers have been given, but this works just fine, does it not?
格式是 ISO,因此
new Date().toISOString()
将为您提供该形式。我正在阅读的内容可能需要进行调整:来源:https:// developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
The formatting is ISO so
new Date().toISOString()
will give you that form. Which as I'm reading might need to be shimmed:Source: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
我发现 moment.js 库非常适合在 javascript 中处理时间。
moment().format()
以 Google API 预期的datetime
格式生成时间戳。或者,为了不依赖于您的应用程序是否正确的默认格式,所有字符串选项(如果您需要的话,包括小数秒): http://momentjs.com/docs/#/displaying/format/
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 adatetime
. Or, to not depend on the default format being correct for your application,All the string options (including fractional seconds if that's what you need): http://momentjs.com/docs/#/displaying/format/
使用
date-fns
,这非常优雅:来源:
Using
date-fns
, this is very elegant:Source:
如果您使用 Google 脚本,另一种选择是使用下面的 Utilities.formatDate URL:
https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
上述 URL 中的示例代码:
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:
试试这个:
来源:http:// /blog.toppingdesign.com/2009/08/13/fast-rfc-3339-date-processing-in-javascript/
try this:
source: http://blog.toppingdesign.com/2009/08/13/fast-rfc-3339-date-processing-in-javascript/
第一个日期后面的 Z 表示它是 UTC (Zulu) 时间,如果没有 Z,它将使用本地(计算机)时间,这可能会相差几个时区。
请参阅:http://en.wikipedia.org/wiki/UTC
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
它看起来更漂亮:
new Date().toISOString().split('.')[0] + 'Z'
It looks more pretty:
new Date().toISOString().split('.')[0] + 'Z'