生成类似于 Google Tasks API 的 RFC 3339 时间戳?

发布于 2024-12-01 21:05:12 字数 791 浏览 0 评论 0原文

我正在构建一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梦一生花开无言 2024-12-08 21:05:12

似乎已经给出了很多复杂的答案,但这很好用,不是吗?

new Date().toISOString()

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

new Date().toISOString()
剩余の解释 2024-12-08 21:05:12

格式是 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

来源: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:

/* 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

怪我太投入 2024-12-08 21:05:12

我发现 moment.js 库非常适合在 javascript 中处理时间。 moment().format() 以 Google API 预期的 datetime 格式生成时间戳。或者,为了不依赖于您的应用程序是否正确的默认格式,

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

所有字符串选项(如果您需要的话,包括小数秒): 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 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/

送君千里 2024-12-08 21:05:12

使用date-fns,这非常优雅:

import { formatRFC3339 } from 'date-fns'

const result = formatRFC3339(new Date(2019, 8, 18, 19, 0, 52))
//=> '2019-09-18T19:00:52Z'

来源:

Using date-fns, this is very elegant:

import { formatRFC3339 } from 'date-fns'

const result = formatRFC3339(new Date(2019, 8, 18, 19, 0, 52))
//=> '2019-09-18T19:00:52Z'

Source:

醉梦枕江山 2024-12-08 21:05:12

如果您使用 Google 脚本,另一种选择是使用下面的 Utilities.formatDate URL:

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

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

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'");
不交电费瞎发啥光 2024-12-08 21:05:12

试试这个:

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;
};

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

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/

燃情 2024-12-08 21:05:12

第一个日期后面的 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

忘你却要生生世世 2024-12-08 21:05:12

它看起来更漂亮:
new Date().toISOString().split('.')[0] + 'Z'

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文