User Tools

Site Tools


javascript:date_object_to_yyyymmdd

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
javascript:date_object_to_yyyymmdd [2016/07/07 19:15] peterjavascript:date_object_to_yyyymmdd [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 16: Line 16:
 </code> </code>
  
 +or
  
  
 +To not modify native objects, and to use multiplication which some think is clearer than string padding use:
  
-This is a single line of code that you can use to create a YYYY-MM-DD string of today's date.+<code javascript> 
 +function yyyymmdd(dateIn) { 
 +   var yyyy = dateIn.getFullYear(); 
 +   var mm = dateIn.getMonth()+1; // getMonth() is zero-based 
 +   var dd  = dateIn.getDate(); 
 +   return String(10000*yyyy + 100*mm + dd); // Leading zeros for mm and dd 
 +
 + 
 +var today = new Date(); 
 +console.log(yyyymmdd(today)); 
 +</code
 + 
 +or 
 + 
 +<code javascript> 
 +Date.prototype.yyyymmdd = function() { 
 +  var yyyy = this.getFullYear(); 
 +  var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based 
 +  var dd  = this.getDate() < 10 ? "0" + this.getDate() : this.getDate(); 
 +  return "".concat(yyyy).concat(mm).concat(dd); 
 +}; 
 + 
 +Date.prototype.yyyymmddhhmm = function() { 
 +  var yyyy = this.getFullYear(); 
 +  var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based 
 +  var dd  = this.getDate() < 10 ? "0" + this.getDate() : this.getDate(); 
 +  var hh = this.getHours() < 10 ? "0" + this.getHours() : this.getHours(); 
 +  var min = this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes(); 
 +  return "".concat(yyyy).concat(mm).concat(dd).concat(hh).concat(min); 
 +}; 
 + 
 +Date.prototype.yyyymmddhhmmss = function() { 
 +  var yyyy = this.getFullYear(); 
 +  var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based 
 +  var dd  = this.getDate() < 10 ? "0" + this.getDate() : this.getDate(); 
 +  var hh = this.getHours() < 10 ? "0" + this.getHours() : this.getHours(); 
 +  var min = this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes(); 
 +  var ss = this.getSeconds() < 10 ? "0" + this.getSeconds() : this.getSeconds(); 
 +  return "".concat(yyyy).concat(mm).concat(dd).concat(hh).concat(min).concat(ss); 
 +}; 
 + 
 +var d = new Date(); 
 +d.yyyymmdd(); 
 +d.yyyymmddhhmm(); 
 +d.yyyymmddhhmmss(); 
 +</code> 
 + 
 + 
 + 
 +===== Using Date.toISOString() ===== 
 + 
 +**WARNING**:  **Date.toISOString()** creates a UTC formatted date and hence can jump date boundaries depending on the timezone. E.g. in GMT+1: (new Date(2013,13, 25)).toISOString() == '2013-12-24T23:00:00.000Z'  
 + 
 +To create a **YYYY-MM-DD** string of today's date:
  
 <code javascript> <code javascript>
 var d = new Date().toISOString().slice(0,10); var d = new Date().toISOString().slice(0,10);
 +</code>
 +
 +
 +or
 +
 +<code javascript>
 +var d = new Date();
 +var res = d.toISOString().slice(0,10).replace(/-/g,"");
 +</code>
 +
 +
 +To get **YYYYMMDDHHmmSSsss**use this: 
 +
 +<code javascript>
 +var ds = (new Date()).toISOString().replace(/[^0-9]/g, "");
 </code> </code>
  
  
javascript/date_object_to_yyyymmdd.1467918925.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki