Converting Javascript Date to year/mt/dy
by Hastig Z
HTML
<div class="activatoror">click to run</div>
CSS
body {
background-color: hsla(0, 0%, 60%, 1);
}
.activatoror {
position: fixed;
top: 50%; left: 50%;
transform: translateY(-50%) translateX(-50%);
background-color: hsla(0, 0%, 20%, 1);
color: white;
padding: 0px 20px;
line-height: 50px;
font-size: 25px;
font-variant: small-caps;
font-weight: bold;
cursor: pointer;
}
.activatoror:hover {
color: hsla(0, 0%, 20%, 1);
background-color: hsla(100, 50%, 50%, 1);
}
JavaScript
// fiddle to complement an answer on stackoverflow
// http://stackoverflow.com/a/39904542/3377049
$('.activatoror').click(function() {
// dateObj can be a javascript formatted date saved elsewhere from another time not being now
// or current time, as below
var dateObj = new Date();
// test date is stored to dateObj
alert('javascript date format: ' + dateObj);
// make month 2 digits (09 instead of 9) months are 0 based so note the +1
var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);
// make date 2 digits
var date = ('0' + dateObj.getDate()).slice(-2);
// get 4 digit year
var year = dateObj.getFullYear();
// concatenate into desired arrangement
var shortDate = year + '/' + month + '/' + date;
// test date is properly formatted
alert('desired format: ' + shortDate);
});