Javascript Date working strange - javascript

I was working with Javascript Date and faced strange problem.
date1 = new Date(1970, 1, 1);
date2 = new Date("1970-01-01T13:00:00.000Z");
console.log(date1.getYear()); //70
console.log(date1.getMonth()); //1
console.log(date1.getDay()); //0 expect 1
console.log(date2.getYear()); //70
console.log(date2.getMonth()); //0 expect 1
console.log(date2.getDay()); //4 expect 1
Why this result happened? What I am doing wrong with Date Object?
FIDDLE
UPDATE:
console.log(date1);
shows this result.
Date 1970-01-31T14:00:00.000Z

With new Date(year, month, date), month is 0 based, so 1 is not January but february, so your date1 and date2 are different dates. Then, the function getDay returns 0 to 6, that corresponds to Monday to Sunday. If you want the date, you have to use getDate instead.

Related

Javascript date method toISOString doesn't return proper date

I had an issue with Date:
I want to get the last day of month
const date = new Date();
const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(lastDayOfMonth); //Wed Nov 30 2022 00:00:00 GMT+0000 (temps universel coordonné)
console.log(lastDayOfMonth.toISOString()) //2022-11-30T00:00:00.000Z
The same code I've run on other computer's browser
I found the same result except console.log(lastDayOfMonth.toISOString()) //2022-11-29T00:00:00.000Z, I got 29 instead of 30?
I don't know why? if anyone knows, could explain us more why Date behave differently on different browser...
var date = new Date();
date.setMonth(date.getMonth()+1);
date.setDate(0);
The setDate with 0 gives back the previous month day

today.getDate works on other days except on Sunday

Example of this code that works normally at a specified date, which is a Saturday.
weeks=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var today = new Date("September 24, 2022 01:15:00");
document.getElementById("current-day").innerHTML=weeks[today.getDay()-1];
<h1 id="current-day"></h1>
As stated in my title above, this code works on other days except only on Sundays, it just says Undefined, why is that?
weeks=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var today = new Date("September 25, 2022 01:15:00");
document.getElementById("current-day").innerHTML=weeks[today.getDay()-1];
<h1 id="current-day"></h1>
Your code logic looks fine but the weeks array should starts with Sunday and avoid using negative 1 with getDay method.
const weeks = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const today = new Date("September 25, 2022 01:15:00");
document.getElementById("current-day").innerHTML = weeks[today.getDay()];
<h1 id="current-day"></h1>
As it states on MDN for the getDay() function:
The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday
So if you subtract 1 you end up with a negative number.

Javascript and setMonth behaving unexpectedly

I am doing datObj.setMonth(1), but the month is being set to March? Isn't 1 supposed to be February? I'm using Chrome 79.
Here's part of code meant to parse dates such as YYYY-MM-DD HH:MM:SS (because safari can't do that natively)
var date = "2020-02-02 23:59:00"
if (typeof date == 'string')
{
var dateParts = date.split(/[:-\s]+/);
if (dateParts.length == 6)
{
dateObj = new Date();
dateObj.setYear(dateParts[0]);
var m = dateParts[1] - 1;
console.log('m= ' + m);
dateObj.setMonth(m);
console.log('after setmonth, date= ' + dateObj);
dateObj.setDate(dateParts[2]);
dateObj.setHours(dateParts[3]);
dateObj.setMinutes(dateParts[4]);
dateObj.setSeconds(dateParts[5]);
}
}
console.log(dateObj);
alert(dateObj);
Your problem, as you figured, is that you're setting the month while the day is still 30. While you could work around that by using setFullYear and pass year, month and day at once, you really should just construct the whole Date object with the right values in the first place:
dateObj = new Date(dateParts[0], dateParts[1]-1, dateParts[2], dateParts[3], dateParts[4], dateParts[5]);
or rather using UTC as the timezone:
dateObj = new Date(Date.UTC(dateParts[0], dateParts[1]-1, dateParts[2], dateParts[3], dateParts[4], dateParts[5]));
Just figured this out before I submitted. Today is January 30th, 2020. I can't change the month to February, because there is no February 30th. So, the code breaks either on the 29th or the 30th day of the month.
In JavaScript, it is advisable to do
dateObj.setMonth(monthNum -1, dayNum)
to set the day and month at the same time to avoid this problem

Javascript newdate function unexpected output

I am confused by the result of the following script and I don't understand why it is what it is:
enddate = '01-02-2020'; //euro format dd-mm-yyyy
datesplit = enddate.split("-");
console.log("datesplit: ", datesplit); //[ '01', '02', '2020' ]
console.log(datesplit[2]); // 2020
console.log(datesplit[1]); // 02
console.log(datesplit[0]); // 01
enddate1 = new Date(datesplit[2],datesplit[1],datesplit[0]);
console.log("enddate 1", enddate1); //output: 2020-03-01T05:00:00.000Z , but I'm expecting 2020-02-01T00:00:00.000Z
That last console log output is what I can't understand. I would appreciate an explanation of why the result is what it is.
JavaScript treats the month as zero-based. So you'll have to -1 your month value to get the right result. As #RobG said, you should use new Date(Date.UTC(...)) to get your date in UTC
let endDate = '01-02-2020' // dd-mm-yyyy;
let [day, month, year] = endDate.split('-');
// Months are zero-based, so -1 to get the right month
month = month - 1;
console.log(day); // '01'
console.log(month);// 1
console.log(year); // '2020'
let newDate = new Date(Date.UTC(year, month, day));
console.log(newDate) // "2020-02-01T00:00:00.000Z"
Given that the other posts seem to have helped you get the right month, have you tried using .toISOString method on the Date object to get the right UTC offset?
The docs on MDN state that the timezone is always zero UTC offset.
You can check Mozilla Documentation
You will see that January is 0, February is 1, and so on. So that's how date works in JavaScript.
You need to convert your month value to Number and then make it "-1". So something like this:
new Date(datesplit[2], (parseInt(datesplit[1], 10) - 1), datesplit[0])

How to find if today day of the month is greater then a particular date on the month with date-fns

How can I find if todays date of the month is greater then 10 with date-fns
Basically I can get the first day of the month with startOfMonth
then add 10 days to that with addDays and then use ifAfter of isBefore.
The code could be looking like that:
greaterThan = 10;
today = new Date();
targetDate = addDays(startOfMonth(today), greaterThan);
isGreater = isAfter(today, targetDate);
Is there a shortcut withing date-fns to achieve in less code lines?
var greaterThan10 = new Date().getDate() > 10;
Cause the (numerical) day of today will be greater (or not) in any other month as well? Or am I missing something?

Categories

Resources