ey dee ey em
ey dee ey em

Reputation: 8593

How to take only part of the javascript timestamp

I have a js timestamp of Tue Sep 30 2014 12:02:50 GMT-0400 (EDT)

with .getTime() I got 1412092970.768

for most cases, its a today's specific time stamp. I wonder, if I could always ONLY pick out the day month and year and hour, min, day will be always stay with 0.

So for our situation, it should become Tue Sep 30 2014 00:00:00 GMT-0400 (EDT).

I wonder what kind of conversion should I be doing? Because seem convert to unix timestamp with getTime() will result in unknown way of calculation... and I can not really find a way to set time like I would do in PHP.

Any fix for this situation?

Thanks

Upvotes: 0

Views: 2544

Answers (2)

Arjan Blok
Arjan Blok

Reputation: 49

An other option is to send epoch to PHP:

JS:

long epoch = System.currentTimeMillis()/1000;

PHP:

   $dt = new DateTime("@$epoch");   
   $dt->format('Y'); //year

Upvotes: 0

Marc B
Marc B

Reputation: 360572

You can create a date object and then zero-out any components you don't need, or create one with the components you specified, e.g.

foo = new Date();
foo.setHour(0);
foo.setMinute(0);

or something more like

foo = new Date(); // "now"

bar = new Date(foo.getYear(), foo.getMonth(), foo.getDate(), 0 , 0, 0, 0);
// create new date with just year/month/day value, and time zeroed-out.

The constructor's args are detailed here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Upvotes: 2

Related Questions