10
var date = "Fri Jan 29 2012 06:12:00 GMT+0100";

How can i show this in format 2012-01-29 06:12 ? In PHP is function ->format. In Javascript is also format, but if i try use this then i have error:

now.format is not a function

var now = new Date();
console.log(now.format("isoDateTime"));

http://jsfiddle.net/6v9hD/

I would like receive format: 2012-01-29 06:12

Mark Fondy
  • 3,773
  • 8
  • 24
  • 25
  • possible duplicate of [How to get current date in jquery?](http://stackoverflow.com/questions/8398897/how-to-get-current-date-in-jquery) – Tadeck Jan 29 '12 at 03:58
  • Not a duplicate because this post includes time formatting – eze Apr 15 '14 at 17:35

6 Answers6

23

This question is a duplicate (see: How to get current date in jquery?).

By modifying my solution from the other question, I got:

var d = new Date();

var month = d.getMonth()+1;
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();

var output = d.getFullYear() + '-' +
    ((''+month).length<2 ? '0' : '') + month + '-' +
    ((''+day).length<2 ? '0' : '') + day + ' ' +
    ((''+hour).length<2 ? '0' :'') + hour + ':' +
    ((''+minute).length<2 ? '0' :'') + minute + ':' +
    ((''+second).length<2 ? '0' :'') + second;

See this jsfiddle for a proof: http://jsfiddle.net/nCE9u/3/

You can also enclose it within function (demo is here: http://jsfiddle.net/nCE9u/4/):

function getISODateTime(d){
    // padding function
    var s = function(a,b){return(1e15+a+"").slice(-b)};

    // default date parameter
    if (typeof d === 'undefined'){
        d = new Date();
    };

    // return ISO datetime
    return d.getFullYear() + '-' +
        s(d.getMonth()+1,2) + '-' +
        s(d.getDate(),2) + ' ' +
        s(d.getHours(),2) + ':' +
        s(d.getMinutes(),2) + ':' +
        s(d.getSeconds(),2);
}

and use it like that:

getISODateTime(new Date());

or:

getISODateTime(some_other_date);

EDIT: I have added some improvement to the function, as proposed by Ates Goral (also decreased its readability in favour of code comments).

Community
  • 1
  • 1
Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • My goodness. This is one verbose implementation :/ You could at least use a local function for the padding: `function pad(n) { return n < 10 ? n : ("0" + n); }` – Ates Goral Jan 29 '12 at 19:06
  • @AtesGoral: You are right about internal function - I have refactored this code. For consistency reasons I made it return string at all times (in your case sometimes it would be an integer). Thanks for pointing this out. – Tadeck Jan 29 '12 at 20:54
  • 1
    This is nice and really should be the first answer that pops up when searching for formatting JS dateTime. Can't believe vanilla JS doesn't have something like this built in - it's crazy we have to have a big-ass function to do something this simple! :P – Jimbo Jan 24 '13 at 10:31
6

Datejs.toString('yyyy-MM-dd HH:mm') should do the trick

Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
3

Unfortunately, in Javascript, Date does not have a format() method.

Check out http://fisforformat.sourceforge.net for some nice formatting methods.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
3

Use a library like Datejs or perhaps this tweet-sized implementation:

https://gist.github.com/1005948

var str = formatDate(
    new Date(),
    "{FullYear}-{Month:2}-{Date:2} {Hours:2}:{Minutes:2}");
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
1

I think this could be help you:date.format.js

var now = new Date();
now.format("m/dd/yy");
// Returns, e.g., 6/09/07
// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM
// You can use one of several named masks
now.format("isoDateTime");
helloyou
  • 11
  • 1
0

You can use something like this(include date.js ) :

Date.parse(yourDate).toISOString();

so the date will have ISO 8601 format.

Condemateguadua
  • 576
  • 5
  • 9