Created
March 1, 2016 15:08
-
-
Save roe3p/126b7953ff7cbcdfb869 to your computer and use it in GitHub Desktop.
Javascript Datetime functions
This file contains 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
/*DATETIME FUNCTIONS | |
Functions to create datetime strings in a specific format. | |
Created to force dates to be passed as SQL parameters in the correct format (i.e. not MM/DD/YYYY) | |
*/ | |
function fDate(date) { | |
//Format date to 'dd/MM/yyyy' | |
if (typeof date == 'undefined') date = new Date(); | |
if (Object.prototype.toString.call(date) === '[object Date]') { | |
var newdate = ('0' + date.getDate()).slice(-2) + '/' | |
+ ('0' + (date.getMonth()+1)).slice(-2) + '/' | |
+ date.getFullYear(); | |
return newdate; | |
} | |
} | |
function fDatetime(date) { | |
//Format datetime to 'dd/MM/yyyy hh:mm:ss' | |
if (typeof date == 'undefined') date = new Date(); | |
if (Object.prototype.toString.call(date) === '[object Date]') { | |
var newdate = ('0' + date.getDate()).slice(-2) + '/' | |
+ ('0' + (date.getMonth()+1)).slice(-2) + '/' | |
+ date.getFullYear() + ' ' | |
+ ('0' + date.getHours()).slice(-2) + ':' | |
+ ('0' + date.getMinutes()).slice(-2) + ':' | |
+ ('0' + date.getSeconds()).slice(-2); | |
return newdate; | |
} | |
} | |
function fDateYMD(date) { | |
//Format date to 'yyyy-MM-dd' | |
if (typeof date == 'undefined') date = new Date(); | |
if (Object.prototype.toString.call(date) === '[object Date]') { | |
var newdate = date.getFullYear() + '-' | |
+ ('0' + (date.getMonth()+1)).slice(-2) + '-' | |
+ ('0' + date.getDate()).slice(-2); | |
return newdate; | |
} | |
} | |
function fDatetimeYMD(date) { | |
//Format datetime to 'yyyy-MM-dd hh:mm:ss' | |
if (typeof date == 'undefined') date = new Date(); | |
if (Object.prototype.toString.call(date) === '[object Date]') { | |
var newdate = date.getFullYear() + '-' | |
+ ('0' + (date.getMonth()+1)).slice(-2) + '-' | |
+ ('0' + date.getDate()).slice(-2) + ' ' | |
+ ('0' + date.getHours()).slice(-2) + ':' | |
+ ('0' + date.getMinutes()).slice(-2) + ':' | |
+ ('0' + date.getSeconds()).slice(-2); | |
return newdate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment