Skip to content

Instantly share code, notes, and snippets.

@jrsalunga
Last active December 20, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrsalunga/6078902 to your computer and use it in GitHub Desktop.
Save jrsalunga/6078902 to your computer and use it in GitHub Desktop.
Javascript Date functions: 1. Get AM/PM Hours 2. Get current day number of current year 3. Get current date. format: yyyy-mm-dd
/*
1.
*/
Date.prototype.getHoursAMPM = function() {
var date = new Date();
var hours = date.getHours();
hours = hours % 12;
hours = hours ? hours : 12;
return hours;
}
/* usage */
var date = new Date();
var x = date.getHoursAMPM()
/*
if the time is 5:00 PM
x = 5
*/
/*
2.
*/
Date.prototype.getDOY = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}
/* usage */
var date = new Date();
var x = date.getDOY()
/*
if the current date is July 15, 2013
x = 206
*/
/*
3.
*/
Date.prototype.currentIsoDate = function() {
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
return year+'-'+month+'-'+day;
}
/* usage */
var date = new Date();
var x = date.currentIsoDate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment