a good practice to handle date

198 views
Skip to first unread message

Angelo Chen

unread,
Mar 20, 2012, 3:42:03 AM3/20/12
to nodejs
Hi,

I use redis to store data, how to save the datetime data in redis that
can be easily used later back to javascript?

tried var d = new Date()
s = d.toISOString()
it saves as a string, but Date.parse(s) seems not converting it back
correctly, any hints?

Thanks,

mscdex

unread,
Mar 20, 2012, 3:59:12 AM3/20/12
to nodejs
On Mar 20, 3:42 am, Angelo Chen <angelochen...@gmail.com> wrote:
> tried var d = new Date()
> s = d.toISOString()
> it saves as a string, but Date.parse(s) seems not converting it back
> correctly, any hints?

Date.parse returns a unix timestamp, so you need `new Date(s)` to get
an actual Date object.

This works for me with v0.6.13:

> var d = new Date()
undefined
> d
Tue, 20 Mar 2012 07:57:16 GMT
> Date.parse(d.toISOString())
1332230236333
> new Date(Date.parse(d.toISOString()))
Tue, 20 Mar 2012 07:57:16 GMT

Matt

unread,
Mar 20, 2012, 10:11:17 AM3/20/12
to nod...@googlegroups.com
Also you can skip the .parse() part in that (although it's done internally, obviously):

> var d = new Date();
undefined
> d
Tue, 20 Mar 2012 14:09:58 GMT
> new Date(d.toISOString());
Tue, 20 Mar 2012 14:09:58 GMT





--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Dale Tan

unread,
Mar 20, 2012, 10:20:15 AM3/20/12
to nod...@googlegroups.com, nod...@googlegroups.com
You can try converting the date to epoch instead and then converting it back to a date (on my iPhone so please excuse any mistakes and psuedo code):

var date = +new Date()
// save it in redis

// retrieve from redis
var date = new Date(redis_date)

I believe that should work. 

--- dale

mscdex

unread,
Mar 20, 2012, 1:08:00 PM3/20/12
to nodejs
On Mar 20, 10:20 am, Dale Tan <wtd...@gmail.com> wrote:
> var date = +new Date()
> // save it in redis

or `Date.now()` :-)

Angelo Chen

unread,
Mar 20, 2012, 8:03:02 PM3/20/12
to nodejs
when a json is json.stringify and parse back to json later, the date
is different:

var d = {"id":1, "d":new Date()}
//{ id: 1, d: Tue, 20 Mar 2012 23:55:10 GMT }

s = JSON.stringify(d)
//'{"id":1,"d":"2012-03-20T23:55:10.352Z"}'

b = JSON.parse(s)
//{ id: 1, d: '2012-03-20T23:55:10.352Z' }

mscdex

unread,
Mar 20, 2012, 8:51:44 PM3/20/12
to nodejs
On Mar 20, 8:03 pm, Angelo Chen <angelochen...@gmail.com> wrote:
> when a json is json.stringify and parse back to json later, the date
> is different:

Right, because there is no special Date type in JSON, it has to
convert it to a string. When parsing that string back again, the JSON
parser can't assume any strings are necessarily a Date, so it just
keeps them as strings. You have to convert those manually. One way to
do this is to pass a callback to JSON.parse() as a second argument,
and it will execute the callback with the key and value as arguments.
Then it's just a matter of returning the original or transformed value
from that callback. You can also returned `undefined` to delete the
current value.

> var s = '{"id":1,"d":"2012-03-20T23:55:10.352Z"}';
undefined
> var o = JSON.parse(s, function(key, val) {
... if (key === 'd') // or perhaps checking val using a regex
... val = new Date(val);
... return val;
... });
undefined
> o

Jorge

unread,
Mar 21, 2012, 8:47:37 AM3/21/12
to nod...@googlegroups.com
On Mar 20, 2012, at 6:08 PM, mscdex wrote:
> On Mar 20, 10:20 am, Dale Tan <wtd...@gmail.com> wrote:
>> var date = +new Date()
>
> or `Date.now()` :-)

Q: Is `Date.now()` equal to `+new Date()` ?
A: Not always.

node << EOF
while (1) {
if (+new Date() === Date.now()) {
process.stdout.write('.');
}
else {
console.log("FAIL!");
break;
}
}
EOF

-> ...FAIL!

Things of the time.

:-P
--
Jorge.

Dave Clements

unread,
Mar 21, 2012, 10:19:50 AM3/21/12
to nod...@googlegroups.com
Are you sure this isn't just a time discrepency between first date and second date?

node << EOF
 while(1) {
 if (Date.now() === Date.now()) {
    process.stdout.write('.');
 }
   else {
     console.log('fail');
     break;
   }
 }
 EOF
.........................................................................................................................................................................................................................................................................................................................................................................................fail

Jorge

unread,
Mar 21, 2012, 11:07:05 AM3/21/12
to nod...@googlegroups.com
Yes, time keeps flowing like a river... :-P
--
Jorge.

Mihai Tomescu

unread,
Mar 21, 2012, 11:34:42 AM3/21/12
to nod...@googlegroups.com

Dean Landolt

unread,
Mar 21, 2012, 2:14:28 PM3/21/12
to nod...@googlegroups.com
On Wed, Mar 21, 2012 at 11:07 AM, Jorge <jo...@jorgechamorro.com> wrote:
Yes, time keeps flowing like a river... :-P

Cute, and true, but it's totally beside the point. You could say this about any impure function. The point is whether their behaviors are equivalent -- or more specifically, whether you could rewrite one into the other. AFAIK the answer is yes.

Marcin Zawadzki

unread,
Mar 23, 2012, 11:28:24 AM3/23/12
to nod...@googlegroups.com
No. Date.now() doesn't create new object, while ne Date() does. So there is memory problem.

Dick Hardt

unread,
Mar 23, 2012, 12:58:50 PM3/23/12
to nod...@googlegroups.com
I store current time in Redis as an integer with:

timeStamp = (new Date()).getTime()

then on retrieval

date = new Date(timeStamp)

to get a Date object

Was easier for me than serializing an object

-- Dick

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to

> nodejs+un...@googlegroups.com

Reply all
Reply to author
Forward
0 new messages