I want to get today’s date with JavaScript in this format: YYYY-MM-DD
Example: 2017-06-12
In PHP, I can get it with the following simple line:
$today = date('Y-m-d');
In JavaScript, I use the following:
var today = new Date();
var day = today.getDate();
if (day < 10) day = '0'+day;
var month = today.getMonth();
if (month < 10) month = '0'+month;
var year = today.getFullYear();**
var todayDate = year+'-'+month+'-'+day;
Is there no simpler way for this like how PHP handles it?
In JavaScript there is no inbuilt date function do show a date in a given format so you have to manually do some coding like what you have shown in the question itself.