var today =newDate();var dd =String(today.getDate()).padStart(2,'0');var mm =String(today.getMonth()+1).padStart(2,'0');//January is 0!var yyyy = today.getFullYear();
today = mm +'/'+ dd +'/'+ yyyy;document.write(today);
let today =newDate().toLocaleDateString();//or functionfunctiongetDate(){let today =newDate();let dd =String(today.getDate()).padStart(2,'0');let mm =String(today.getMonth()+1).padStart(2,'0');//janvier = 0let yyyy = today.getFullYear();return`${yyyy}-${mm}-${dd}`;//return dd + '/' + mm + '/' + yyyy; // change form if you need}
convert a new date standard to a yyy-mm-dd format in javascript
functionconvert(str){var date =newDate(str),
mnth =("0"+(date.getMonth()+1)).slice(-2),
day =("0"+ date.getDate()).slice(-2);return[date.getFullYear(), mnth, day].join("-");}console.log(convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)"))//-> "2011-06-08"
var dateString ="20120515";var year = dateString.substring(0,4);var month = dateString.substring(4,6);var day = dateString.substring(6,8);var date =newDate(year, month-1, day);
javascript yyyy-mm-dd to mm-dd-yyyy human readable format
functionformatDate(DB_date){// Get Date from DB then format it to be human readable// DB_date comes in as yyyy-mm-dd which throws off the date format by one dayvar ymdSplit =DB_date.split('-');// so we split it into an array and then we can use the array to create a new date object// console.log(ymdSplit[0])// console.log(ymdSplit[1])// console.log(ymdSplit[2])var d =newDate(""+ymdSplit[1]+"/"+ymdSplit[2]+"/"+ymdSplit[0]+"");// re-assembles the date object into a string// console.log(d)var wd = d.getDay();var mo = d.getMonth();let weekdays =["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];let months =["January","February","March","April","May","June","July","August","September","October","November","December"];// console.log(weekdays[wd])// console.log(months[mo])// return this function so it comes back as: Tuesday, March 15th, 2022 :) all done!return weekdays[wd]+", "+ months[mo]+" "+ ymdSplit[2]+", "+ ymdSplit[0];}