I have a function that converts a date to YYYY-MM-DD from DD/MM/YYYY.
This works in all browsers apart from IE8, for some reason, when creating a new Date object, it is returning NaN.
Basic implementation of the code /
var pareDate = function(value){
var dateFragements = value.split('/');
if (dateFragements.length == 3) {
var currentDate = new Date();
currentDate.setHours(0, 0, 0, 0);
var startDate = new Date(dateFragements[2] + '-' + dateFragements[1] + '-' + dateFragements[0]);
if (startDate >= currentDate) {
return true;
} else {
return false;
}
}
}
alert(pareDate('17/09/2013'));
I have a function that converts a date to YYYY-MM-DD from DD/MM/YYYY.
This works in all browsers apart from IE8, for some reason, when creating a new Date object, it is returning NaN.
Basic implementation of the code http://jsfiddle/bX83c/1/
var pareDate = function(value){
var dateFragements = value.split('/');
if (dateFragements.length == 3) {
var currentDate = new Date();
currentDate.setHours(0, 0, 0, 0);
var startDate = new Date(dateFragements[2] + '-' + dateFragements[1] + '-' + dateFragements[0]);
if (startDate >= currentDate) {
return true;
} else {
return false;
}
}
}
alert(pareDate('17/09/2013'));
Share
Improve this question
edited Mar 18, 2014 at 12:07
Dairo
8181 gold badge9 silver badges24 bronze badges
asked Sep 5, 2013 at 9:16
CharliePrynnCharliePrynn
3,0805 gold badges42 silver badges70 bronze badges
1
- I think you have your answer in one of the following: stackoverflow./questions/2182246/… or: stackoverflow./questions/11020658/… Simple googling... – Mircea Botez Commented Sep 5, 2013 at 9:28
3 Answers
Reset to default 3Intialise your date like this. It will work in all browsers
var startDate = new Date(dateFragements[2] , dateFragements[1] , dateFragements[0]);
There are 4 ways in which Date
object can be intialised using constructor
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
String in Date object doesn't mean it will accept all date strings. If you want to give a string as input give this. (dateFragements[2] +'/' + dateFragements[1] + '/' + dateFragements[0]);. (/
as the separator) It will be supported in all browsers
IE8 expects '/'
as the separator in a date string, that's why your function fails.
It can be simplified to:
var pareDate = function(value){
var dateFragements = value.split('/');
if (dateFragements.length == 3) {
var currentDate = function(){ return (this.setHours(0),
this.setMinutes(0),
this.setSeconds(0),
this); }.call(new Date)
,startDate = new Date([dateFragements[2],
dateFragements[1],
dateFragements[0]].join('/'));
return startDate>=currentDate;
}
}
new Date(dateString)
accepts the following formats (only):
"October 13, 1975 11:13:00"
"October 13, 1975 11:13"
"October 13, 1975"