Last active
December 20, 2015 05:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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