简体   繁体   中英

Extract Month and Day in Javascript

I have the date in this format: 1347564203.713372

And need to end up with 2 variables, one that is the month from that date and another that is the day from that date.

How do I do this using Javascript/jQuery?

This should do:

var myDate = 1347564203.713372;
var d= new Date(myDate*1000);
var month = d.getMonth();
var day = d.getDate();

Create a Date object, use setTime to put your timestamp in there, then get the relevant parts:

var d = new Date(), t = 1347564203.713372;
d.setTime(t*1000); // JS uses timestamps in milliseconds
alert(d.getUTCDate());
alert(["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][d.getUTCMonth()]);

Note use of getUTC* functions - this helps avoid timezone issues and DST.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM