I'm attempting to calculate the day of the year (either with today or another date).
In PHP I can use the date() function and do whatever I want...
Is there something similar in JS/jQuery?
Surprised nobody posted about moment.js, it's very similar to date.js, but lighter (5.5k minified)
http://momentjs.com/
Just did a benchmark and found it parses much faster than date.js, but formatting and manipulating is slower. For one-liners it'd execute faster than date.js due to the faster parsing.
http://jsperf.com/underscore-date-vs-datejs/7
Some usage examples:
// Simulates ajax response
var data = { someDate: "2023-08-23 11:52:39" }
// .unix() converts to Unix timestamp: 1692816759
moment(data.someDate).unix();
// Displaying it in a readable format
// Aug 23, 11:52 AM
moment("2023-08-23 11:52:39").format('MMM D, hh:mm A');
// Now
moment();
// Support for manipulation and chaining
moment().add('days', 7).subtract('months', 1).hours(15).minutes(0).seconds(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With