Javascript Date Object Off by One Month - javascript

I understand that javascript counts the months starting from 0. However, I don't know how to account for this when getting a date in the future.
For example, when attempting to get the date for tomorrow, the following code returns everything correct except for the month (prints as next month):
const weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var today = new Date();
today.setUTCDate(today.getUTCDate()+1);
let newDate = weekday[today.getDay()] +' '+today.getDate()+' '+month[today.getDay()]+', '+ today.getFullYear();
console.log(newDate)

Just replace today.getDay() to today.getMonth()
const weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var today = new Date();
today.setUTCDate(today.getUTCDate()+1);
let newDate = weekday[today.getDay()] +' '+today.getDate()+' '+month[today.getMonth()]+', '+ today.getFullYear();
console.log(newDate)

Related

Wrong Date displayed

I have the following in an html file to display today's Date.
<p id="todays-date">
<script>
var currentDate = new Date();
var day = currentDate.getDay();
var month = currentDate.getMonth();
var year = currentDate.getFullYear();
var fullDate = day + "/" + month + "/" + year;
document.write(fullDate);
</script>
</p>
As for today being the 23rd of September 2021 23/9/2021 in Australia
the result I am getting is 4/8/2021.
Any help would be appreciated.
MiltonT.
getDay returns the current weekday and getMonth returns the current month, starting from 0
I would recommend you to take a look at the Date API docs
var currentDate = new Date();
var day = currentDate.getDate(); // Returns the day of the month (1–31) for the specified date according to local time.
var month = currentDate.getMonth() + 1; // Returns the month (0–11) in the specified date according to local time.
var year = currentDate.getFullYear();
var fullDate = day + "/" + month + "/" + year;
document.write(fullDate);
getDay gives you the day of the week, not the date. use getDate instead.
getMonth is zero-based. (january is 0, not 1). Add 1 to get the conventional one-based month.
Replace getDay with getDate, getDay returns the day of the week
0 represent Sunday and 6 represent Saturday. So 4 here in your question represent thursday
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth();
var year = currentDate.getFullYear();
var fullDate = day + "/" + month + "/" + year;
document.write(fullDate);
Try this
var currentDate = new Date();
console.log(currentDate.toLocaleString('en-US'));
// 9/23/2021, 10:28:46 AM
YOu will get output in above format
Hello the mistake you did here is you fetched day instead of date.
The second mistake is months count from 0 to 11 instead of 1-12 increment by 1 will solve your problem
here is modified version of your code
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth()+1;
var year = currentDate.getFullYear();
var fullDate = day + "/" + month + "/" + year;
window.alert(fullDate);
js months calculate from 0-11 instead of 1-12

How to change format to standard time JavaScript

I have a date string in JavaScript which is arranged in a particular manner. How do I rearrange this in order to fit standard time?
let date = "2020-06-01T00:00:00Z"
How do I rearrange the date variable in order to match the format MM/DD, YYYY ?
Change your code to as follows:
let date = "2020-06-01T00:00:00Z";
date = new Date(date);
var dd = date.getDate();
var mm = date.getMonth()+1;
var yyyy = date.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm};
console.log(mm+'/'+dd+', '+yyyy)
i don't real know this is possible but i found the function you can use instead
function GetFormattedDate() {
var todayTime = new Date();
var month = todayTime.getMonth() + 1;
var day = todayTime.getDate();
var year = todayTime.getFullYear();
return month + "/" + day + "/" + year;
}
console.log(GetFormattedDate());
so you can add more in the function or edit what the function will return as a format according to your will

how to compare a date with system date in javascript

I have to put a validation check that compares the date (ex:12-jun-2015) with the current system date in javascript. I am able to compare the date and year,but since the month is in the 'mon' format, so I am not able to compare with system date.
var date = new Date();
var currentMonth = date.getMonth();
var dateFields = (document.getElementById('startDate1').value.split('-'));
var screenMonth = dateFields[1];
Please suggest
This can be accomplished by using a month array containing all months code in 'mon' format.
var month = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
var date = new Date();
var currentMonth = date.getMonth();
var dateFields = (document.getElementById('startDate1').value.split('-'));
var screenMonth = dateFields[1];
if(sceenMonth.toLowerCase()==month[currentMonth])
// same month
else
// not same month

HTML5 get date string convert to date object

I try to use html5 type="date" and get the string and convert it to Date() in JS.
var dateString = $(this).prev().val();
var date = new Date(dateString);
var day = date.getDay();
var month = date.getMonth();
var year = date.getYear();
finalDate = day + "/" + month + "/" + year;
alert(finalDate);
But the result I got is different than what I've set, I have no idea what's wrong here :
I expect to get 18/05/1991
My demo is here http://jsfiddle.net/yL1q3ygf/
getMonth() returns a number between 0 and 11. You want to use getMonth()+1.
getDay() returns the day of the week, you want to use getDate() instead.
use getUTCDate(); before making it as an object (date).
var d = new Date(dateString);
var n = d.getUTCDate();
then use n in your code. it will work
$(function(){
$('button').click(function(){
var dateString = $(this).prev().val();
var date = new Date(dateString);
var day = date.getDate(); //day is for day of week. use date
var month = date.getMonth() +1; // months are zero based
var year = date.getFullYear(); //use full year for 4 digit year
finalDate = day + "/" + month + "/" + year;
alert(finalDate);
});
});
I updated your jsfiddle script
http://jsfiddle.net/yL1q3ygf/5/

Current month (number) + year (number)

How do I output the current year and month (in numbers) in javascript?
getUTCMonth() logs: getUTCMonth is not defined
getUTCMonth() logs: getMonth is not defined
you could do it like
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
Attention
date.getMonth() starts with 0 and goes to 11 so you have to add +1
Did you try this ?
var date = new Date();
date.getUTCMonth();
date.getUTCFullYear();
Month:
new Date().getMonth()
Year:
new Date().getFullYear()

Categories

Resources