Why does my JavaScript date show wrong day of the month? [duplicate] - javascript

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Date.getDay() javascript returns wrong day
(8 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
So I just started programming (I don't know if this is a correct word for it). I started making a website and wanted to add a date and clock. I followed a YouTube tutorial and it works but the month number is one month behind. Is there anyway to fix this problem? Also I wanted to make month number double if its number is less than 10. I wrote the JavaScript code and it messed with the month name (In this case Gegužė).

The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
const moonLanding = new Date('July 20, 69 00:20:18');
console.log(moonLanding.getMonth()); // (January gives 0)
// expected output: 6

Related

How can I check in javascript if it's day time or night time on the client? [duplicate]

This question already has answers here:
Check if current time is between two given times in JavaScript
(5 answers)
Closed 3 years ago.
Using date-nfs, how can I check if new Date() is between 8am and 8pm?
You don't need to use external libraries, you can simply get the current hour by new Date().getHours(), and check if it's more then 20 or less then 8.
getHours() on the date object will be an integer between 0 and 23. So you could check if it's between 8 and 20

Why date().getMonth() return wrong month [duplicate]

This question already has answers here:
unexpected javascript date behavior
(3 answers)
Closed 3 years ago.
I need to get the current date from the browser for my app,
when I am calling new Date().getMonth() In google chrome console it shows me'9' like this :
Why happen this?
The months in JavaScript are zero indexed, so January is 0 and December is 11. That makes October 9.
Date.getMonth() gives one less the actual month. If you want to show proper month, define an array like 'var month=[Januaary,february....]' this, then access the month like this month[Date().getMonth()].

I can't get Month and Day in Date Javascript [duplicate]

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!

In Momentjs, how to parse dates sent from server built in Java? [duplicate]

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.

javascript add X number of months to date keeping the day number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to add months to a date in JavaScript?
What's the simplest way to add X numbers of months to a date object but keep the day number? My date's will always be the first of the month.
Examples:
If my start date is "04/01/2012" and i need to add 4 months to it, the result will be "08/01/2012".
If my start date is "10/01/2012" and i need to add 4 months to it, the result will be "02/01/2013".
Try something like date.setMonth(date.getMonth()+4). This will automatically catch the overflow condition and add one to the Year.

Categories

Resources