Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a PHP date() equivalent in javascript/jquery? [closed]

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?

like image 715
Will Avatar asked Nov 11 '11 20:11

Will


1 Answers

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);
like image 69
Scott Yang Avatar answered Oct 13 '22 12:10

Scott Yang