生成类似于 Google Tasks API 的 RFC 3339 时间戳?
我正在构建一个与 Google Tasks 同步的应用.作为同步的一部分,我想比较本地任务和 API 任务,看看哪个最近发生了变化.
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 API 的每个任务都包含一个 updated
属性,如下所示:
Each task from Google's API contains an updated
property, which looks like this:
2011-08-30T13:22:53.108Z
现在我想生成一个类似的时间戳,这样每次我更新应用程序上的任务时,它都会设置一个新的 updated
值.要生成我正在使用的 RFC 3339 时间戳 - http:///cbas.pandion.im/2009/10/generating-rfc-3339-timestamps-in.html 生成如下内容:
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
问题是,API 日期总是以大于"本地日期的形式返回,即使本地日期较新也是如此.我猜这与两者之间的不同格式有关.
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.
这里有两个日期,顶部来自 Google Tasks API(大约 10 分钟前),底部是一分钟前在本地生成的.当比较哪个更大时,它告诉我最高的是.
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.
推荐答案
格式是 ISO,所以 new Date().toISOString()
会给你那个格式.正如我正在阅读的那样,可能需要填充:
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
来源:https://developer.mozilla.org/en/JavaScript/参考/Global_Objects/日期
相关文章