Javascript : Date & Time
Get Time
var robsDate = new Date();
console.log(robsDate.getTime()) //returns the number of milliseconds since January 1, 1970 (i.e. 1508518545394)
console.log(robsDate.getMilliseconds()) //returns 0-999 for current millisecond
console.log(robsDate.getSeconds()) //returns 0-59 for current second
console.log(robsDate.getMinutes()) //returns 0-59 for current minute
console.log(robsDate.getHours()) //returns 0-23 for current hour
Get Date
var robsDate = new Date();
console.log(robsDate.getDay()) //returns 0 if its Sunday etc.
console.log(robsDate.getDate()) //returns 1 if its the firt day of the month etc.
console.log(robsDate.getMonth()) //returns 0 if its January etc.
console.log(robsDate.getFullYear()) //returns current year etc. (i.e. 2017)
Date Conversion 1
Convert date string "1/31/2017 1:53:51 PM" to "1-31-2017"
var dateIn = "1/31/2017 1:53:51 PM"
var space1 = dateIn.indexOf(" ");
var date = dateIn.substring(0, space1);
var arr = date.split("/");
var m = arr[0]-1; // January is 0
var d = arr[1];
var y = arr[2];
Pretty Date
function returnPrettyDate(uglyDate) {
// in: 2017-10-20 | out: October 20, 2017
// in 0000-00-00 | out: ""
if (uglyDate === "0000-00-00" || uglyDate === "") {return "date unavailable";}
var arr = uglyDate.split("-");
var year = arr[0];
var month = "";
var day = arr[2];
switch(Number(arr[1])){
case 1: month = "January"; break;
case 2: month = "February";break;
case 3: month = "March";break;
case 4: month = "April";break;
case 5: month = "May";break;
case 6: month = "June"; break;
case 7: month = "July";break;
case 8: month = "August";break;
case 9: month = "September";break;
case 10: month = "October";break;
case 11: month = "November";break;
case 12: month = "December";break;
}
return month + " " + day + ", " + year;
}