This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript is creating date wrong month
In my code, I create Date by this line:
var date = new Date('2012', '01', '20')
and then using:
alert(date);
I get the result:
I don't really understand why not Jan instead of Feb?
jsFiddle: http://jsfiddle.net/U5m8N/
The "Month" part of var date = new Date('2012', '01', '20'); is zero-indexed.
Start counting at 0, and you got your month. (So, January is 0, Feb 1, etc.)
Also, while JavaScript does accept strings as parameters, you should be using integers, as the documentation suggests:
Date Documentation
Javascript Date Object's Month is 0 indexed. http://www.w3schools.com/jsref/jsref_obj_date.asp. put 0 instead of 1 for Jan
Related
This question already has answers here:
javascript date conversion showing wrong month
(3 answers)
javascript is creating date wrong month
(4 answers)
Closed 2 years ago.
Ok, I have a weird issue. I'm just starting with Node and MongoDB and at first I got a little bit confused by the whole ISO time vs local time and I ran into a weird issue.
I got a string with a date, and although it's overkill, I manually splitted it and pass it to the "new Date()" constructor, it adds one month to all the dates.
This is my code:
str = str.split(' ')
str[0] = str[0].split('-')
str[1] = str[1].split(':')
console.log(str)
str = new Date(str[0][0],str[0][1],str[0][2],str[1][0],str[1][1],str[1][2]);
console.log(str)
console.log(str.toString())
And this is the output:
[ [ '1970', '01', '01' ], [ '00', '00', '00' ] ] //First UNIX date splitted into a yyyy, mm, dd, HH, MM, DD
1970-02-01T03:00:00.000Z //After the Date object is generated, note that the month is now "02"
Sun Feb 01 1970 00:00:00 GMT-0300 (hora de verano de Chile) //The date returned to local time. Note that the month change was maintained.
Am I doing something wrong in changing types? Does it have to do with something aside the code?
The whole purpose is to insert the Date object into a mongodb collection in ISO format, and then query it back and show it in local time.
According to the documentation for Date, the month is zero-indexed. So it's not adding a month, you're just passing the wrong value because the month in your string is one-indexed.
Subtract 1 from that value:
new Date(str[0][0], str[0][1] - 1, str[0][2], str[1][0], str[1][1], str[1][2]);
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I am trying to convert this date into Mon Nov 26 2018 10:32:04 GMT (I am getting this data from the Api so i can't make changes to it)
I assume it is considering 26 as months thats why it is showing it as invalid date
Can Anyone help me with this. How to convert that date into the expected output i specified.
How to get
var d = new Date("26-11-2018 10:32:04")
return d; //Error: Invalid Date
expected Output: Mon Nov 26 2018 10:32:04 (IST)
Use moment.js to parse the date.
moment("26-11-2018 10:32:04", "DD-MM-YYYY HH-mm-ss").toDate()
Alternatively, if you really don't want to use moment for whatever reason, you can use regex magic.
new Date("26-11-2018 10:32:04".replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, "$3-$2-$1T$4:$5:$6Z"))
This is not a robust as #Yevgen answer but it also much simpler.
All I'm doing is removing the - and flipping the day and month values
const items = "26-11-2018 10:32:04".split('-')
new Date(`${items[1]} ${items[0]} ${items[2]}`)
This works for personal projects but I highly recommend using moment.js
This question already has answers here:
Javascript returns wrong Date values (NodeJS)
(3 answers)
Closed 4 years ago.
I am try get month and day but value is different o my text:
var fech = new Date("2021-02-28T00:00:00");
document.writeln("Year=" + fech.getFullYear());
document.writeln("Month=" + fech.getMonth());
document.writeln("Day=" + fech.getDay());
Result is:
Year=2021 Month=1 Day=0
https://jsfiddle.net/j0qn2jjr/3/
getDay() returns the day of the week (Sunday is 0, Monday is 1, etc). What you're probably looking for is getDate().
Also, getMonth() returns 0-11 (January is 0, February is 1, etc).
Read up here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
GetMonth() returns the number month, which in this instance in Feb, using a 0 index, Feb would actually be month 1 in this scenario. Use getMonth()+1 if you need to get the numbers.
#Máté Safranka has explained about the day problem too!
This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 5 years ago.
In a project I need to convert in javascript a timestamp into a human readable string, however the number of month was incorrect so I performed the following test in a javascript console :
How to explain why there is a one month difference ?
Thank you in advance
Javascript dates start at 0 for months
example:
0: Jan
1: Feb
2: Mar
etc
In javascript months start from 0. So January is 0, and december is 11
docs
Return value An integer number, between 0 and 11, representing the
month in the given date according to local time. 0 corresponds to
January, 1 to February, and so on.
Javascript's date.getMonth starts the month count from 0. So you need to add +1.
var date = new Date();
console.log(date.getMonth() + 1);
This question already has answers here:
Javascript Date.UTC() function is off by a month?
(4 answers)
Closed 5 years ago.
I get dates from server as a list, for example, [2017,8,24,9,0]. When I parse and localize them in momentjs, all the dates are one month ahead: instead of August, I get September. In case of [2017,8,31,9,0], I get invalid date.I think it is because September is not 31 days.
My question is how to parse dates such as [2017,8,24,9,0], [2017,8,29,20,0], and [2017,8,31,9,0] into D.MMM [kl.] H:mm format?
I understand that JavaScript date is zero-indexed while java date isn't. I have used momentjs subtract() method, but [2017,8,31,9,0] is still invalid date.
That's a javascript issue. But is not a bug. In js dates, months are zero based. It is, 0 = january, 1 = february, etc. To solve this you can subtract the month by 1.
var myServerDate = [2017,8,24,9,0];
myServerDate[1] = myServerDate[1] -1;
Then you can proceed with the parsing process.