How to create the date in YYYYMMDDHHMMSS format using JavaScript ? For example, I want to get the date as 20131018064838.
Asked
Active
Viewed 9.3k times
24
-
I would use the library I suggested [here](http://stackoverflow.com/questions/17734032/date-format-convert-javascript/17734119#17734119) anyway it would help a lot if you share your actual code too. – Adriano Repetti Oct 18 '13 at 11:23
-
11(new Date()).toISOString().replace(/[^0-9]/g, "").slice(0, -3) – MBS Aug 14 '17 at 12:47
-
@MBS yours is the aptest answer – Sumith Jul 25 '20 at 10:22
-
another solution: `dt=new Date().toISOString().split('.')[0].replace(/[^\d]/gi,'');` sets `dt` to, (for example): `20210809154700` – ashleedawg Aug 09 '21 at 15:46
-
1Or `new Date.toISOString().replace(/(\.\d{3})|[^\d]/g,'')` – József Takó Jan 21 '22 at 07:37
-
With https://github.com/d3/d3-time-format , `timeFormat("%Y-%m-%d %H:%M:%S")(yourDate)` – Eric Jun 20 '22 at 03:00
3 Answers
21
var date = new Date();
alert( date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2) + ("0" + date.getHours() ).slice(-2) + ("0" + date.getMinutes()).slice(-2) + ("0" + date.getSeconds()).slice(-2) );
edit
function pad2(n) { return n < 10 ? '0' + n : n }
var date = new Date();
alert( date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2( date.getDate()) + pad2( date.getHours() ) + pad2( date.getMinutes() ) + pad2( date.getSeconds() ) );

alltom
- 3,162
- 4
- 31
- 47

gurvinder372
- 66,980
- 10
- 72
- 94
-
neat as the `('0' + n).slice(-2)` hack is, IMHO it's not very readable and the OP would be far better served with a little `pad2` helper function, e.g. `function pad2(n) { return n < 10 ? '0' + n : n }` – Alnitak Oct 18 '13 at 11:25
-
-
2
-
-
2@gurvinder372 congrats - you now have the only correct (standalone) answer! – Alnitak Oct 18 '13 at 11:29
-
-
@gurvinder372 actually there's one further change required - at least _one_ of the fields (suggest the `year` field) needs to be forced into a string to ensure that you get string concatenation instead of addition. – Alnitak Oct 18 '13 at 11:37
-
-
-
-
-
I'd do it in a one-liner like `new Date().toISOString().replace(/[^0-9]/gm, "").substr(0,14)`. – Mig82 Oct 04 '19 at 21:47
10
Here's my (ES5 safe) method to add the YYYYMMDDHHMMSS()
function to any Date
object.
On older browsers, either shim Object.defineProperty
or just add the inner function directly to Date.prototype
:
Object.defineProperty(Date.prototype, 'YYYYMMDDHHMMSS', {
value: function() {
function pad2(n) { // always returns a string
return (n < 10 ? '0' : '') + n;
}
return this.getFullYear() +
pad2(this.getMonth() + 1) +
pad2(this.getDate()) +
pad2(this.getHours()) +
pad2(this.getMinutes()) +
pad2(this.getSeconds());
}
});

Alnitak
- 334,560
- 70
- 407
- 495
-
9A bit subjective perhaps, but (assuming timezone being zero UTC offset is fine, ) I think `new Date().toISOString().slice(-24).replace(/\D/g,'').slice(0, 14);` is cleaner since it involves no tinkering with zero-padding or correction of the month number. – StubbornShowaGuy Sep 15 '16 at 05:07
6
Please try using prototype method as following.
<script type="text/javascript">
Date.prototype.YYYYMMDDHHMMSS = function () {
var yyyy = this.getFullYear().toString();
var MM = pad(this.getMonth() + 1,2);
var dd = pad(this.getDate(), 2);
var hh = pad(this.getHours(), 2);
var mm = pad(this.getMinutes(), 2)
var ss = pad(this.getSeconds(), 2)
return yyyy + MM + dd+ hh + mm + ss;
};
function getDate() {
d = new Date();
alert(d.YYYYMMDDHHMMSS());
}
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
</script>

masud_moni
- 1,121
- 16
- 33

mit
- 1,763
- 4
- 16
- 27
-
that's a novel (but inefficient) way of zero padding the numbers... You also omitted to zero pad the time fields. – Alnitak Oct 18 '13 at 11:34
-
1getting better, but in this case there's no need to pad beyond two digits, and you could then make `pad2` (per the other answer that uses it) an inner function within `Date.prototype.YYYYMMDDHHMMSS` to avoid polluting the global name space. – Alnitak Oct 18 '13 at 11:48
-
1thanks for code snippets, I've more customized for debugging purpose. https://jsfiddle.net/trustfarm/nvvzgax1/4/ – cpplover - Slw Essencial Apr 09 '18 at 11:45