{title}


Code RSS Feed

Code

Get an ISO 8601 Formatted Date in Javascript

TOPICS:
  • Javascript

Recently I had to submit an ISO 8601 timestamp in order to use Amazon’s AWS API - unfortunately there’s no cut and dry method of doing this in javascript like there is in other languages. I finally came across this method, which I figured I’d share.

function ISODateString(d){
          
function pad(n){return n<10 '0'+n}
          
return d.getUTCFullYear()+'-'
              
pad(d.getUTCMonth()+1)+'-'
              
pad(d.getUTCDate())+'T'
              
pad(d.getUTCHours())+':'
              
pad(d.getUTCMinutes())+':'
              
pad(d.getUTCSeconds())+'Z'
        
}
        
        
        
function getTimeStamp(){
            
var = new Date();
            var 
now ISODateString(d);
            
console.log(now);
        
}

getTimeStamp
()