Gusgus
Gusgus

Reputation: 353

Generate and format current date and time

How to do the following in JavaScript?:

$curr_time_start = new DateTime();
$timeStart = date_format($curr_time_start, 'Y-m-d H:i');

Upvotes: 0

Views: 128

Answers (4)

Nicolás Ozimica
Nicolás Ozimica

Reputation: 9758

Natively it's not possible to do such a thing with Javascript.

There are many ways to accomplish that using frameworks or specific libraries.

For example, with MooTools you can do this:

var timeStart = new Date().format('%Y-%m-%d %H:%M');

You can test this with this JsFiddle.

The syntax for the format is almost the same, but for the %M instead of i for the minutes, and because all the placeholders are prefixed by %.

Upvotes: 1

Tom Fobear
Tom Fobear

Reputation: 6749

Probably something like

var d = new Date();
alert(
    d.getYear() + '-' + d.getMonth() + '-' + d.getDay() + ' ' + 
    d.getHours() + ':' + d.getMinutes());

javascript Date()

Upvotes: 1

Jashwant
Jashwant

Reputation: 29025

Try date.js

Datejs is an open-source JavaScript Date Library.

Comprehensive, yet simple, stealthy and fast. Datejs has passed all trials and is ready to strike. Datejs doesn’t just parse strings, it slices them cleanly in two.

Upvotes: 0

Brian Hoover
Brian Hoover

Reputation: 7991

I normally use the plugin from Matt Kruse -

http://javascripttoolbox.com/lib/date/

Upvotes: 1

Related Questions