Converting string time into milliseconds/Affect font styling

154 views
Skip to first unread message

jose banks

unread,
Mar 3, 2013, 5:56:33 PM3/3/13
to d3...@googlegroups.com
I have a dataset and each has a time stamp for when it was created in this exact format e.g. 
   
     [ {"Name": "Jake", "created":"2013-03-01T19:54:24Z" },
       {"Name": "Rock", "created":"2012-03-01T19:54:24Z" } ]

As a result I wish to use 'created' within a function which calculates that if the data was entered 60days or less from today it will appear in italic. However, the function I've attempted has no affect. I'm attempting to do the calculations all in milliseconds:

         node.append("text")
        .text(function(d) { return d.Name; })
         .style("font", function (d) 
                 { var ja = Date.parse(d.created);
                     var time = new Date ();
                    var n = time.getTime();
   
                      if(ja > (n - 5184000)) {return "Arial 10px bold";}
                     else {return "Arial 11px";}
                 })
        .text(function(d) { return d.Name; });

I have also tried to replace var ja with:

var iso = d3.time.format.utc("%Y-%m-%dT%H:%M:%S.%LZ").parse(d.created)


I am curious whether I am actually converting the data at all into milliseconds. Or if my formatting is completely wrong! As my text doesnt change at all... Even when I change the font size, it doesnt change. So I'm a bit confused.

Here's an example: http://jsfiddle.net/xwZjN/84/

Thanks in advance

Simon Russell

unread,
Mar 3, 2013, 6:01:42 PM3/3/13
to d3...@googlegroups.com
You're comparing a Date with a number -- you perhaps want to do a
getTime after the parse.
> --
> You received this message because you are subscribed to the Google Groups
> "d3-js" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to d3-js+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

jose banks

unread,
Mar 3, 2013, 6:09:50 PM3/3/13
to d3...@googlegroups.com
Hi Simon,

like so... 

  var q = ja.getTime; ??

Simon Russell

unread,
Mar 3, 2013, 6:15:15 PM3/3/13
to d3...@googlegroups.com
Yes, but:

var q = ja.getTime();

Or just inline:

if (ja.getTime() > ... etc)

Simon Russell

unread,
Mar 3, 2013, 6:17:28 PM3/3/13
to d3...@googlegroups.com
Wow, terrible, I'm totally wrong. Sorry.

I was thinking of the d3 time parsing stuff.

Apologies again. I missed the jsfiddle link actually, so I'll have a
look on there.

Simon Russell

unread,
Mar 3, 2013, 6:25:00 PM3/3/13
to d3...@googlegroups.com
Right. Several things:

- you need to be comparing milliseconds, not seconds: 5184000000 (60
days in milliseconds)
- at least in my browser, the font style is a lot more strict than in
HTML: "bold 40px Arial" works, but not if you change the order.
- in your sample, none of your dates are older than 60 days :)

jose banks

unread,
Mar 3, 2013, 6:39:43 PM3/3/13
to d3...@googlegroups.com
Hi Simon, 

Thanks! All this time the font style was in the wrong order - so annoying! Thanks for clearing things up! :)

Simon Russell

unread,
Mar 3, 2013, 6:40:50 PM3/3/13
to d3...@googlegroups.com
Yeah, it took me a while to see that one ... I don't tend to use the
composite styles.
Reply all
Reply to author
Forward
0 new messages