javascript:date_object_to_yyyymmdd
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
javascript:date_object_to_yyyymmdd [2016/07/07 19:13] – created peter | javascript:date_object_to_yyyymmdd [2020/07/15 09:30] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 15: | Line 15: | ||
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)); | ||
+ | </ | ||
+ | |||
+ | or | ||
+ | |||
+ | <code javascript> | ||
+ | Date.prototype.yyyymmdd = function() { | ||
+ | var yyyy = this.getFullYear(); | ||
+ | var mm = this.getMonth() < 9 ? " | ||
+ | var dd = this.getDate() < 10 ? " | ||
+ | return "" | ||
+ | }; | ||
+ | |||
+ | Date.prototype.yyyymmddhhmm = function() { | ||
+ | var yyyy = this.getFullYear(); | ||
+ | var mm = this.getMonth() < 9 ? " | ||
+ | var dd = this.getDate() < 10 ? " | ||
+ | var hh = this.getHours() < 10 ? " | ||
+ | var min = this.getMinutes() < 10 ? " | ||
+ | return "" | ||
+ | }; | ||
+ | |||
+ | Date.prototype.yyyymmddhhmmss = function() { | ||
+ | var yyyy = this.getFullYear(); | ||
+ | var mm = this.getMonth() < 9 ? " | ||
+ | var dd = this.getDate() < 10 ? " | ||
+ | var hh = this.getHours() < 10 ? " | ||
+ | var min = this.getMinutes() < 10 ? " | ||
+ | var ss = this.getSeconds() < 10 ? " | ||
+ | return "" | ||
+ | }; | ||
+ | |||
+ | var d = new Date(); | ||
+ | d.yyyymmdd(); | ||
+ | d.yyyymmddhhmm(); | ||
+ | d.yyyymmddhhmmss(); | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Using Date.toISOString() ===== | ||
+ | |||
+ | **WARNING**: | ||
+ | |||
+ | To create a **YYYY-MM-DD** string of today' | ||
+ | |||
+ | <code javascript> | ||
+ | var d = new Date().toISOString().slice(0, | ||
+ | </ | ||
+ | |||
+ | |||
+ | 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.1467918837.txt.gz · Last modified: 2020/07/15 09:30 (external edit)