javascript:date_object_to_yyyymmdd
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
javascript:date_object_to_yyyymmdd [2016/07/07 19:20] – peter | javascript:date_object_to_yyyymmdd [2020/07/15 09:30] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 14: | Line 14: | ||
var date = new Date(); | var date = new Date(); | ||
date.yyyymmdd(); | date.yyyymmdd(); | ||
+ | </ | ||
+ | |||
+ | or | ||
+ | |||
+ | |||
+ | To not modify native objects, and to use multiplication which some think is clearer than string padding use: | ||
+ | |||
+ | <code javascript> | ||
+ | function yyyymmdd(dateIn) { | ||
+ | var yyyy = dateIn.getFullYear(); | ||
+ | var mm = dateIn.getMonth()+1; | ||
+ | var dd = dateIn.getDate(); | ||
+ | | ||
+ | } | ||
+ | |||
+ | var today = new Date(); | ||
+ | console.log(yyyymmdd(today)); | ||
</ | </ | ||
Line 53: | Line 70: | ||
+ | ===== Using Date.toISOString() ===== | ||
- | This is a single line of code that you can use to create a YYYY-MM-DD string of today' | + | **WARNING**: |
+ | |||
+ | To create a **YYYY-MM-DD** string of today' | ||
<code javascript> | <code javascript> | ||
Line 60: | Line 80: | ||
</ | </ | ||
+ | |||
+ | or | ||
+ | |||
+ | <code javascript> | ||
+ | var d = new Date(); | ||
+ | var res = d.toISOString().slice(0, | ||
+ | </ | ||
+ | |||
+ | |||
+ | To get **YYYYMMDDHHmmSSsss**use this: | ||
+ | |||
+ | <code javascript> | ||
+ | var ds = (new Date()).toISOString().replace(/ | ||
+ | </ | ||
javascript/date_object_to_yyyymmdd.1467919201.txt.gz · Last modified: 2020/07/15 09:30 (external edit)