Skip to content

How can I save a ISO 8601 UTC date in Datastore with a date and time index #599

Description

@byrneciaran

Let's say I have some JSON with an ISO 8610 UTC date as follows:

{
    name: 'Meeting'
    start: '2013-05-14T13:01:00.234Z'
}

How do I save this in Datastore with a date and time index created for the start property ?

I know in the googleapis node module I can create an entity like:

var entity = {
    key: { path: [{ kind: 'Employee' }] },
    properties: {
        name: { stringValue: 'Meeting' },
        start: { dateTimeValue: '2013-05-14T13:01:00.234Z' },
    }
};

But, in gcloud-node, it seems I can only do something like:

{
    name: 'Meeting'
    start: new Date()
}

which gives me a Javascript Date object in local time not a ISO 8601 UTC Date.

Activity

  1. jgeewax commented on May 14, 2015

    @jgeewax
    Contributor

    I think what you want is...

    { start: Date.parse('2013-05-14T13:01:00.234Z'); }

    or if you use Moment.js:

    { start: moment('2013-05-14T13:01:00.234Z').toDate(); }

    /cc @ryanseys As I think he had some docs that explained this, but I don't see them live on the gh-pages hosted site.

  2. added
    type: questionRequest for information or clarification. Not an issue.
    api: datastoreIssues related to the Datastore API.
    on May 14, 2015
  3. added this to the Datastore Stable milestone on May 14, 2015
  4. byrneciaran commented on May 14, 2015

    @byrneciaran
    Author

    Hi @jgeewax, I don't think that will work as Date gives me a back a Date object in my local timezone (GMT+0100), which is then stored in the Datastore in UTC format.

    I want to pass in a UTC date string to begin with using something like moment.utc().toJSON()

        var localTZ = moment('2013-05-14T13:01:00.234Z').toDate();
        console.log(localTZ);  // Tue May 14 2013 14:01:00 GMT+0100 (IST) <- Date Object Local TZ
        console.log(localTZ.toJSON()); // 2013-05-14T13:01:00.234Z <- UTC
    
        var utc = moment.utc('2013-05-14T13:01:00.234Z'); <- UTC
        console.log(utc.toJSON()); // 2013-05-14T13:01:00.234Z <- UTC
    
  5. jgeewax commented on May 14, 2015

    @jgeewax
    Contributor

    Hm, I think this is a question of broader scope than gcloud-node...

    Any objection to taking this over to Stack Overflow? I think it's probably going to get answered better there.. You effectively need to get a timezone-aware Javascript Date object... I believe that once you have that, gcloud-node will handle it properly, right @ryanseys ?

  6. ryanseys commented on May 14, 2015

    @ryanseys
    Contributor

    Yep.

  7. ryanseys commented on May 14, 2015

    @ryanseys
    Contributor

    This might be a bug. I'll look into it now.

  8. ryanseys commented on May 14, 2015

    @ryanseys
    Contributor

    Nope doesn't look like a bug in our code, we just get the number of milliseconds since the last Epoch using:

    var d = new Date(Date.parse('2013-05-14T13:01:00.234Z')); // set start to this object.
    d.getTime() // num of milliseconds since epoch - no notion of timezone here

    then multiply by 1000 to get microseconds and set that value to timestamp_microseconds_value.

    Here's showing that the date is properly parsed as UTC (same as GMT):

    > new Date(Date.parse('2013-05-14T13:01:00.234Z'))
    Tue May 14 2013 09:01:00 GMT-0400 (EDT)
    > new Date(Date.parse('2013-05-14T13:01:00.234Z')).toUTCString()
    'Tue, 14 May 2013 13:01:00 GMT'
  9. ryanseys commented on May 14, 2015

    @ryanseys
    Contributor

    To answer your original question, try this:

    {
        name: 'Meeting'
        start: new Date(Date.parse('2013-05-14T13:01:00.234Z'))
    }
  10. ryanseys commented on May 24, 2015

    @ryanseys
    Contributor

    @byrneciaran any update on whether this helped you?

  11. stephenplusplus commented on Jun 4, 2015

    @stephenplusplus
    Contributor

    I think we this one is covered, but @byrneciaran, feel free to re-open if it didn't work. Thanks for getting us to double check how we handle dates!

  12. 23 remaining items

  13. added a commit that references this issue on Jan 28, 2026
  14. added a commit that references this issue on Feb 17, 2026
  15. added a commit that references this issue on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions