GeezerVison is showing github.com — How can I save a ISO 8601 UTC date in Datastore with a date and time index · Issue #599 · googleapis/google-cloud-node
How can I save a ISO 8601 UTC date in Datastore with a date and time index #599DescriptionLet'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. Activityjgeewax commented on May 14, 2015 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. byrneciaran commented on May 14, 2015 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
jgeewax commented on May 14, 2015 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 ? ryanseys commented on May 14, 2015 ryanseys commented on May 14, 2015 This might be a bug. I'll look into it now. ryanseys commented on May 14, 2015 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'ryanseys commented on May 14, 2015 To answer your original question, try this:
{
name: 'Meeting'
start: new Date(Date.parse('2013-05-14T13:01:00.234Z'))
}ryanseys commented on May 24, 2015 stephenplusplus commented on Jun 4, 2015 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!
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|