Get an ISO 8601 Formatted Date in Javascript

AWS requires it, and JS doesn't make it intuitive, so here's how

Javascript, Date Formatting, Time

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 : 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 d = new Date();
            var now = ISODateString(d);
            console.log(now);
        }

getTimeStamp()