This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 2 years ago.
I want to represent the first of a given month, say 1st of Septemeber 2020. When I do this in my console, I get the below output:
$ node
> new Date(2020, 8, 1)
2020-08-31T23:00:00.000Z
How come it returns 2020-08-31 instead of 2020-08-01? What am I doing wrong?
The output
2020-08-31T23:00:00.000Z
is ISO format, and the Z shows the date is in UTC. When you call the Date constructor, it is using your browser timezone by default, which I can assume is +01:00. Moreover, month indice in the constructor is zero-based.
If what you want is the beginning of the month in UTC, you can do:
d = new Date(Date.UTC(2020, 8, 01))
d.toISOString()
"2020-09-01T00:00:00.000Z"
try
new Date(2020, 7, 1)
because date object is count from 0-11 no 1-12
Related
This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 1 year ago.
I try to use the date object from NodeJS v14.15.1. And when I instantiate the object with the year, month and day parameter. When I try to use the getDay() and getYear() method, I don't have the right number.
I really don't know what going on and why this method don't return what's I expected.
const date = new Date(2021, 4, 1) // I create a date object for 2021/04/01
console.log(date.getDay()) // This return 6 instead of 1
console.log(date.getYear()) // This return 121 instead if 2021
There's some unfortunate historical naming of methods here. Check out MDN's documentation for Date.
getDay returns the day of the week (Sunday = 0, Monday = 1, etc.). You probably want to use getDate instead.
Similarly, getYear returns the year minus 1900. You probably want to use getFullYear instead.
Although you didn't mention it, note that getMonth is zero-based: 0 = January, 1 = February, etc. The same is true for new Date. So new Date(2021, 4, 1) corresponds to the ISO date 2021-05-01.
This question already has answers here:
Creating java date object from year,month,day
(6 answers)
java.util.Date and getYear()
(12 answers)
Closed 2 years ago.
Currently I am trying to convert a date calculation from javascript to java, since I want to use it for Android development. So in javascript I was using the method date.valueOf(), which converts the date to the milliseconds that passed since January 1, 1970. In java the method with the same funcionality is called date.getTime(). In the java and javascript documentation the description is exactly the same, but when I am inserting the same date, I am getting two completely different values.
For example:
Java getTime()
Date date = new Date(2011, 10, 1);
System.out.println(date.getTime()); //prints: 61278249600000
Javascript valueOf()
const date = new Date(2011, 10, 1);
console.log(date.valueOf()); //prints: 1320102000000
So my questions are:
Why is this happening? (or what did I wrong?)
How can I correct the code?
It would be awesome if somebody can help me.
The discrepancy isn't between Java's getTime and JavaScript's valueOf, it's the Java Date constructor. The year parameter is years since 1900. So the equivalent Java code would be:
Date date = new Date(110, 10, 1);
// ^−−− 2010 - 1900
This is one of the many reasons not to use the java.util.Date class.
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
The Date.getMonth() returns different values for same date in different formats.
The first statement is in the UTC format.
The second one uses YYYY,M,DD format.
I didn't expect it to return a different value because it's the same date.
What is going on?
console.log(new Date('July 20, 69 00:20:18').getMonth()); // returns 6
console.log(new Date(1969, 7, 20).getMonth()); // returns 7
For your first example - read MDN docs for Date.getMonth(). It returns a zero-indexed numerical month representation where January is represented by 0, and December by 11. Thus, July is represented (correctly) by 6.
The second one doesn't use a format per se, it's passing the values as parameters to the Date constructor. The MDN docs for Date() state that the parameter you're passing with a value 7 is also a monthIndex. Thus, to correctly notate July, pass 6 instead.
The getMonth method returns a zero based value. Meaning 0 indicates the first month. Creating a new Date object like in your second example follows the same principle.
See these examples.
The months in a Date object are zero indexed. That means they range from 0 to 11.
new Date('July 20, 69 00:20:18') creates a Date with the month of July which has the index 6.
new Date(1980, 7, 20) creates a date with the month with the 7th index, which is August.
Here you can find more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
That diference happens because when using integer parameters to new Date(1980, 6, 20)
the month parameter starts in 0, not 1. example:
0 - january
1 - february
...
5 - july
6 - august
...
11 - december
So your result is
var dateString = new Date('July 20, 69 00:20:18') // "1969-07-20T03:20:18.000Z"
var dateInt = new Date(1980, 7, 20) // "1980-08-20T03:00:00.000Z"
console.log(dateString.getMonth()); // 6
console.log(dateInt.getMonth()); // 7
This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 4 years ago.
I can get day no via current date like this, it's give me current result
const dateObject = new Date()
console.log(dateObject.getDay())
But When I try to day no of a specific date, I'm not getting actual answer
const dateObject = new Date(2018, 8, 5)
console.log(dateObject.getDay())
I expect 0 , as it's sunday of the week, but I got 3.
What is my fault or misunderstanding here?
Thanks
The monthIndex parameter in the Date constructor zero-based, so 8 is September, not August. getDay then correctly tells you that the fifth of September is a Wednesday.
See MDN for more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters
There are enough bizarre oddities with JS dates that I just use MomentJS whenever I'm dealing with date/time values.
You are wrong with month index. Month mormally start with 0 index.
January = 0
February = 1
March = 2
April = 3
...
...
...
...
So August month's index number is 7.
You answer will be like this:
const dateObject = new Date(2018, 7, 5)
console.log(dateObject.getDay())
In new Date(2018, 8, 5), month starts from 0 and not 1. Hence in your case it is not August but September.
This question already has answers here:
Convert dd-mm-yyyy string to date
(15 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I have this
let d = new Date("03-08-2018"); //dd-mm-yyyy
console.log(d.getMonth()); // returns 02, I want 07
I have my date in dd-mm-yy. The getMonth() thinks I'm using mm-dd-yy.
How do I get correct month and date.
Your date format is not standard, and I strongly recommend not to use such code on a client web code, because how it behaves would be client-dependent.
EDIT (thx RobG) : Don't use the builtin parser new Date(), this will get you into trouble depending on the client timezone, even if you use a proper format where the month seems to be correctly recognized.
You'll have to create some function to parse the string yourself and create a Date object with year, month and day manually set, that would be the safe way.
Example of manual parsing :
let regex = /^(\d{2})-(\d{2})-(\d{4})$/; // each parsing group is day-month-year
let date = '03-05-2018';
let match = regex.exec(date);
let month = +match[2] - 1; // starting at 0, remove -1 if you want 1-based month number
console.log(month); // returns 4 in this example '03-05-2018'
(of course you should also put some guards if the string is not matching the correct format)
JS Date give the month starts with 0. So Try this below for getting month from date
let d = new Date("03-05-2018"); //dd-mm-yyyy
console.log(d.getMonth()+1);
if you want 4, then you should add +2 ( but i am not sure why you want 4)
let d = new Date("03-05-2018"); //dd-mm-yyyy
console.log(d.getMonth()+2);