Created
August 22, 2018 13:02
-
-
Save stevenkuipers/79acd6a75e8d0e5c49b35e958a2a024c to your computer and use it in GitHub Desktop.
js function that returns a date string dd-mm-yyyy and a function to compare two of these strings
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
// returns a date string in format dd/mm/yyyy | |
function dateOfToday(){ | |
var today = new Date(); | |
var dd = today.getDate(); | |
var mm = today.getMonth()+1; //January is 0! | |
var yyyy = today.getFullYear(); | |
if(dd<10){ | |
dd='0'+dd; | |
} | |
if(mm<10){ | |
mm='0'+mm; | |
} | |
today = dd+'/'+mm+'/'+yyyy; | |
return today | |
} | |
// Takes two strings as input format is dd-mm-yyyy | |
// parses them and returns true if d1 is smaller of equal to d2 | |
function compareDates(d1, d2){ | |
var parts =d1.split('/'); | |
var d1 = Number(parts[2] + parts[1] + parts[0]); | |
parts = d2.split('/'); | |
var d2 = Number(parts[2] + parts[1] + parts[0]); | |
return d1 <= d2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment