0 points

JavaScript Dates


dashboard

Fri Oct 09 2020 18:32:00 GMT+0000 (UTC)

Posted by @narro ##js ##javascript ##vanillajs ##dates ##formatting

//* DATE
let currentYear = new Date().getFullYear().toString()
let currentMonth = (new Date().getMonth() +1).toString()
let currentDay = new Date().getDate().toString()
currentMonth.length == 1 ? currentMonth = '0'+currentMonth : currentMonth = currentMonth.toString()
currentDay.length == 1 ? currentDay = '0'+currentDay : currentDay = currentDay.toString()
let yyyymmddToday = [currentYear, currentMonth, currentDay].join("-");
console.log('yyyymmddToday', yyyymmddToday);




// Get date
let date = new Date();

// Format variable named 'date' as MM/DD/YYYY
formatDateMDY() {
  return (
    this.date.slice(0, 10).split("-")[1] +
    "/" +
    this.date.slice(0, 10).split("-")[2] +
    "/" +
    this.date.slice(0, 10).split("-")[0]
    );
   },


// Get today's date in the default format string: '2020-10-11T00:00:00+05:30'
let date = new Date();

// Format as MM/DD/YYYY
let monthDateYear = ((date.getMonth() > 8) ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1))) + '/' + ((date.getDate() > 9) ? date.getDate() : ('0' + date.getDate())) + '/' + date.getFullYear();
console.log(monthDateYear)

// Pad Numbers (as seen in UnixIsoDate)
let zeroFill = ( number, width ) => {
  width -= number.toString().length;
  if ( width > 0 )
  {
    return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
  }
  return number + ""; // always return a string
}

// Unix date to ISO
let unixToIsoDate = (unixDate) => {
  let month = new Date(unixDate * 1000).getUTCDate()
  let day = new Date(unixDate * 1000).getUTCMonth()
  let year = new Date(unixDate * 1000).getUTCFullYear()
  // YYYY-MM-DD: return [year, zeroFill(month,2), zeroFill(day,2)].join('-')
  // m/d/y: return [month, day, year].join('/')
}
content_copy Copy

Vanilla JS

https://www.notion.so/narrollc/Product-Ideas-a496930f62834fbe8070a70d597cf03b