60

Here is what I need to do.

Get Date, convert to string and pass it over to a third party utility. The response from the library will have date in string format as I passed it. So, I need to convert the date to string like 20110506105524 (YYYYMMDDHHMMSS)

function printDate() {
    var temp = new Date();
    var dateStr = temp.getFullYear().toString() + 
                  temp.getMonth().toString() + 
                  temp.getDate().toString() +
                  temp.getHours().toString() + 
                  temp.getMinutes().toString() + 
                  temp.getSeconds().toString();

    debug (dateStr );
}

The problem with above is that for months 1-9, it prints one digit. How can I change it to print exactly 2 digits for month, date ...

Kiran
  • 5,478
  • 13
  • 56
  • 84
  • 1
    have you looked at this previous one: http://stackoverflow.com/questions/610406/javascript-printf-string-format – Liv May 06 '11 at 16:07
  • 1
    For reference, here is a definitive source: [Javascript Date Object](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date) – Nate Barr Dec 19 '12 at 16:41

6 Answers6

67

You will need to pad with "0" if its a single digit & note that getMonth returns 0..11 not 1..12

function printDate() {
  const temp = new Date();
  const pad = (i) => (i < 10) ? "0" + i : "" + i;

  return temp.getFullYear() +
    pad(1 + temp.getMonth()) +
    pad(temp.getDate()) +
    pad(temp.getHours()) +
    pad(temp.getMinutes()) +
    pad(temp.getSeconds());
}

console.log(printDate());
Alex K.
  • 171,639
  • 30
  • 264
  • 288
39

Relying on JQuery Datepicker, but it could be done easily:

var mydate = new Date();
$.datepicker.formatDate('yy-mm-dd', mydate);
Stéphane
  • 2,068
  • 22
  • 28
  • 2
    How to get time info with datepicker? – Siddharth Apr 28 '12 at 16:48
  • The [Timepicker Addon](http://trentrichardson.com/examples/timepicker/) seems to do the trick . Otherwise, I think the [jquery-timepicker](http://jonthornton.github.com/jquery-timepicker/) looks great – Stéphane Nov 30 '12 at 09:43
6

use this polyfill https://github.com/UziTech/js-date-format

var d = new Date("1/1/2014 10:00 am");
d.format("DDDD 'the' DS 'of' MMMM YYYY h:mm TT");
//output: Wednesday the 1st of January 2014 10:00 AM
Tony Brix
  • 4,085
  • 7
  • 41
  • 53
2

I like Daniel Cerecedo's answer using toJSON() and regex. An even simpler form would be:

var now = new Date();
var regex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*$/;
var token_array = regex.exec(now.toJSON());
// [ "2017-10-31T02:24:45.868Z", "2017", "10", "31", "02", "24", "45" ]
var myFormat = token_array.slice(1).join('');
// "20171031022445"
jammon
  • 3,404
  • 3
  • 20
  • 29
1

A little bit simpler using regex and toJSON().

var now = new Date();
var timeRegex = /^.*T(\d{2}):(\d{2}):(\d{2}).*$/
var dateRegex = /^(\d{4})-(\d{2})-(\d{2})T.*$/
var dateData = dateRegex.exec(now.toJSON());
var timeData = timeRegex.exec(now.toJSON());
var myFormat = dateData[1]+dateData[2]+dateData[3]+timeData[1]+timeData[2]+timeData[3]

Which at the time of writing gives you "20151111180924".

The good thing of using toJSON() is that everything comes already padded.

Daniel Cerecedo
  • 6,071
  • 4
  • 38
  • 51
1

Maybe it is easier to convert the Date into the actual integer 20110506105524 and then convert this into a string:

function printDate() {
    var temp = new Date();
    var dateInt =
        ((((temp.getFullYear() * 100 + 
            temp.getMonth() + 1) * 100 + 
           temp.getDate()) * 100 +
          temp.getHours()) * 100 + 
         temp.getMinutes()) * 100 + 
        temp.getSeconds();

    debug ( '' + dateInt );  // convert to String
}

When temp.getFullYear() < 1000 the result will be one (or more) digits shorter.

Caution: this wont work with millisecond precision (i.e. 17 digits) since Number.MAX_SAFE_INTEGER is 9007199254740991 which is only 16 digits.

jammon
  • 3,404
  • 3
  • 20
  • 29