I just working on date comparison but face something strange with date. my code is something like below code
var now = new Date();
var from = new Date(2013,12,18,7,41,25,0);
var untill = new Date(2013,12,18,8,42,25,0);
if(now <= untill && now >= from){
...
}else{
....
}
this condition in a right time for example 2013/12/18 7:42:00 doesn't work.
I get further investigate about it with console.log :
console.log(from);
console.log(now);
console.log(untill);
the output was as strange as below:
Date {Sat Jan 18 2014 07:41:25 GMT+0330 (Iran Standard Time)}
Date {Wed Dec 18 2013 08:20:22 GMT+0330 (Iran Standard Time)}
Date {Sat Jan 18 2014 08:42:25 GMT+0330 (Iran Standard Time)}
As you can see it's a date for from and untill variable is 2014 January 18 in spite of entered 2013 / 12 / 18
Please help me to figure out why javascript act such this.
The month argument uses 0-based indexing.
Use var from = new Date(2013,11,18,7,41,25,0); for December.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
In Javascript months are 0-indexed, so January is 0, December is 11.
Change your code to
var from = new Date(2013,11,18,7,41,25,0);
var untill = new Date(2013,11,18,8,42,25,0);
You are getting this because the Month and the Day of the week These are based on 0-based indexing.
you can read about the Date in more detail here. Date in Javascript.
Related
using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)
using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)
When I am using javascript date to convert from its default format to Indian format it shows 'Invalid Date' error.
The format is as follows:
var indDate=new JsSimpleDateFormat("dd-MM-yyyy");
When using the following format,
indDate.format(new Date("12-09-2014")) //convert correctly as "09-12-2014"
But when using this one,
indDate.format(new Date("13-09-2014")) //shows "NaN-NaN-0NaN"
Up to date value 12 is working correctly. But after that (from 13) not working properly.
How to solve that. Thanks in advance...
If you want to use the date object then this will work:
var year = 2014, month = 9, day = 13;
var date = new Date(year, month - 1, day); // month is zero based
console.log(date); // prints: Sat Sep 13 2014 00:00:00 GMT+0300 (Eastern Europe Daylight Time)
I dont have a reference to "JsSimpleDateFormat" so I dont really know what it does.
First try is in IE 9 console:
new Date('2013-10-24T07:32:53')
Thu Oct 24 07:32:53 UTC+0200 2013
returns as expected
Next try is in FireFox 24 console:
new Date('2013-10-24T07:32:53')
Date {Thu Oct 24 2013 07:32:53 GMT+0200 (Central Europe Standard Time)}
Then I go into Chrome 30 console:
new Date('2013-10-24T07:32:53')
Thu Oct 24 2013 09:32:53 GMT+0200 (Central Europe Daylight Time)
But the time is 09 here, it should be 07.
Is this a bug in chrome or am I doing something wrong here?
I can't use any other format than this '2013-10-24T07:32:53' that I get by JSON from C#.
I need to get the hour of this timestamp, with the getHours I get the incorect value in Chrome.
Solution:
var inputHour = input.split('T')[1];
inputHour = inputHour.substring(0, 2);
Its no bug. The implementation of date parse function differs across browsers & so does the format of the dateString accepted by it.
However this format seems to work same across ... link:
new Date("October 13, 1975 11:13:00")
If possible, try and use
new Date(year, month, day, hours, minutes, seconds, milliseconds)
for guaranteed results.
Regarding your format try parsing it yourself. Something like :
var str = '2013-10-24T07:32:53'.split("T");
var date = str[0].split("-");
var time = str[1].split(":");
var myDate = new Date(date[0], date[1]-1, date[2], time[0], time[1], time[2], 0);
Note (Thanks to RobG for this) : The Date constructor used above expects month as 0 - 11 & since October is 10 as per date String, the month has to be modified before passing it to the constructor.
Reference.
See this thread:
Why does Date.parse give incorrect results?
It looks like the behavior of the parsing signature of the Date constructor is completely implementation dependent.
Given:
var s = '2013-10-24T07:32:53';
in ES5 compliant browsers you could do:
var d = new Date(s + 'Z');
but for compatibility across all browsers in use, better to use (assuming date is UTC):
function dateFromString(s) {
s = s.split(/\D/);
return new Date(Date.UTC(s[0],--s[1],s[2],s[3],s[4],s[5]));
}
I have a date saved to the var mydate. When i print that i get the string equivalent of Tue Feb 10 2009 00:00:00 GMT-0800 (Pacific Standard Time).
Now i need to format the var mydate so it will display the date as Tue Feb 10 2009.
The code;
var today = $(this).datepicker('getDate');
var mydate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay());
When i display my date, i got the string equivalent of Tue Feb 10 2009 00:00:00 GMT-0800 (Pacific Standard Time). But what i want is this Tue Feb 10 2009. How can i do this by formatting var my date ?
Have you tried myDate.toDateString()?
Here's an example:
> new Date().toDateString()
"Fri Feb 22 2013"
Also have a look at moment.js.
From your code I am assuming you are using the jQuery date picker from http://www.kelvinluck.com.
Assuming you are, and you are using the Date class provided with the package you can do something like this:
//Date.format = 'dd/mm/yyyy';
Date.format = 'D M dd yyyy';
//Tue Feb 10 2009
$('.date-picker').datePicker({
startDate:'01/01/2001'
//other options here..
});
One way that you could achieve this is by creating a custom date (by creating a new class called myDate which extends Date) and override the .toString() method. That it returns the information in the format you desire.
There are good and simple libraries to format dates easily.
You can try this one:
http://www.mattkruse.com/javascript/date/