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.
Related
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])
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.
This is what I have tried. Am getting 21 which is definitely not right. What am I doing wrong?
var today = new Date();
var TrumpsStateVisit = new Date('March 12, 2018 12:00:00');
var daysTillTrumpsStateVisit = TrumpsStateVisit.getTime() - today.getTime();
daysTillTrumpsStateVisit = (daysTillTrumpsStateVisit / 864000000); //number of milleseconds in a day
var $countDownTillTrumpsChinaTrip = ('#countDownTillTrumpsChinaTrip');
$countDownTillTrumpsChinaTrip.textContent = Math.floor(daysTillTrumpsStateVisit);
The main reason this doesn't work is that the number of milliseconds in a day is 100*60*60*24=86400000. You also should construct the date differently as that will be March 12, 2018 12:00 in whatever timezone the users browsers in which might not be what you intend.
However there a number of settle issues related to time changes and other issues when subtracting dates so you should really use a library like https://momentjs.com/ that specializes in handling dates and times.
You were so close!
The main problem is that there aren't 864000000 milliseconds in a day, there are only 86400000; you had one zero too many.
A secondary problem occurs with Daylight Savings; not every day has exactly 24 hours in it, so you need to round the result rather than floor it:
var today = new Date();
var TrumpsStateVisit = new Date('March 12, 2018 12:00:00');
var daysTillTrumpsStateVisit = TrumpsStateVisit.getTime() - today.getTime();
daysTillTrumpsStateVisit = (daysTillTrumpsStateVisit / 86400000); //number of milleseconds in a day
console.log(Math.round(daysTillTrumpsStateVisit));
Hope this helps! :)
I'm trying to read a date from a Calendar object and add a specific amount of days to it (i.e. 7)
Here is the code that I have:
var daysFromBeginDate = parseInt($("#policyDuration").val()) * 7
alert(daysFromBeginDate)
var beginDate = new Date("2015-04-24");
alert(beginDate)
var endDate = new Date("2015-05-08");
alert(beginDate.getDate() + daysFromBeginDate)
endDate.setDate(new Date(beginDate.getDate() + daysFromBeginDate));
alert(endDate.toString())
and I am getting Sun May 31 2015 17:00:000 GMT as my answer. It should be that with one less month, where is the extra month getting added?
JavaScript using the below call, I found out that the month argument counts starting from zero.
new Date(2015, 3, 1); // that's the 1st April 2015!
And what is causing the issue is the below code snippet in your code:-
endDate.getMonth() + 1
That is possibly the reason for your issue..
EDIT:
if the below code
var endDate = new Date("2015-05-08");
is changed to
var endDate = new Date();
you will get correct output..
it is because setDate sets the day of the month and April has only 30 days so it is rolled over and you get it as May and you get 31 because 24+7 is 31..
Why both dates in the below code have same valueOf() and getTime()?
<script>
var endDt = new Date(2014,10,31);
var endDt2 = new Date(2014,11,1);
alert("getTime()\nendDt : "+endDt.getTime()+"\nendDt2: "+endDt2.getTime());
alert("valueOf()\nendDt : "+endDt.valueOf()+"\nendDt2: "+endDt2.valueOf());
</script>
We can see that both values is equals.
I want to get the values to lock the user if user try a interval greater than 31 days.
But when the user puts start Date(2014,10,01) and end Date(2014,11,1) the javascript interprets as end Date(2014,10,31). When i do calc. with difference between start date and end date both values is the same.
<script>
var startDt = new Date(2014,10,01);
var endDt = new Date(2014,10,31);
var endDt2 = new Date(2014,11,1);
var diff = endDt.getTime()-startDt.getTime();
var diff2 = endDt2.getTime()-startDt.getTime();
alert("getTime()\ndiff: "+diff+"\ndiff2: "+diff2);
diff = endDt.valueOf()-startDt.valueOf();
diff2 = endDt2.valueOf()-startDt.valueOf();
alert("valueOf()\ndiff: "+diff+"\ndiff2: "+diff2);
</script>
Why are these values coming up the same, even though the provided dates are different?
You are creating the wrong dates. Months are 0-based in JavaScript, so new Date(2014,10,31); is (theoretically) Nov 31st and new Date(2014,11,1) is Dec 1st.
Of course Nov 31st doesn't exist, so it's correct to Dec 1st.
From the big yellow box in the MDN documentation:
Note: Where Date is called as a constructor with more than one argument, if values are greater than their logical range (e.g. 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. E.g. new Date(2013, 13, 1) is equivalent to new Date(2014, 1, 1), both create a date for 2014-02-01 (note that the month is 0-based).