JSFiddle - React, Tailwind, and code Playground

by ecropolis

HTML

<div id="output"></div>
<div id="output-fixed"></div>

JavaScript

var d = 1458931311043;

$('#output').html(normalizeDateBAD(d));
$('#output-fixed').html(normalizeDate(d));

/* this is the original function */
function normalizeDateBAD(dateValue) {
  var now = new Date(dateValue);
  var month = now.getMonth() + 1;
  return (now.getFullYear() + '-' + month + '-' + now.getDate());
}

/* this new version will add leading zeros to month and day */
function normalizeDate(dateValue) {
  var now = new Date(dateValue);
  var month = ('0'+(now.getMonth() + 1)).slice(-2);
  return (now.getFullYear() + '-' + month + '-' + ('0'+now.getDate()).slice(-2) );
}