Wrong Date displayed - javascript

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

Related

How to convert a date formatted (YYMMDD) as string but with 00 days?

I have a date formatted as string, eg: 240800. The date format for that string is YYMMDD. With the below code, I can convert the string to date but it doesn't always work in deducting 1 day. I need my output to be a valid date, not with 00 day. So with the date above, it should be converted and formatted to 07/31/2024.
Here's what I got so far.
function formatDate(stringDate) {
var year = stringDate.substring(0,2);
var month = stringDate.substring(2,4);
var day = stringDate.substring(4,6);
var date = new Date('20' + year, month, day);
var formattedDate = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear();
console.log(formattedDate);
}
Working:
"240800" = 7/31/2024
All months from 4 to 12
Not Working:
"240100" = 0/31/2024 x
"240200" = 1/29/2024 x
"240300" = 2/31/2024 x
The reason is the date variable parameter in new Date() is counted as 0~11, not the general range,1~12.
So the working answer actually is wrong. It seems like being right just for July and August have 31 days.
The correct way is to firstly deduct 1 month and then calculate it. After all of the process is done, you can add 1 month in the end.
The below is working codes:
function formatDate(stringDate) {
var year = stringDate.substring(0,2);
//deduct 1 month firstly
var month = Number(stringDate.substring(2,4))-1;
var day = stringDate.substring(4,6);
var date = new Date('20' + year, month, day);
//add 1 month finally
var formattedDate = date.getMonth()+1 + '/' + date.getDate() + '/' + date.getFullYear();
console.log(formattedDate);
}
formatDate('240100');
In python Assuming your string is yymmdd below function should do what you want. I am sure javascript has some module for date handling.
from datetime import datetime, timedelta
def fd(s):
d=datetime.strptime(s[:-2],'%y%m')+timedelta(days=int(s[-2:])-1)
return d.strftime('%m/%d/%Y')
Try this ..
function formatDate(stringDate) {
var year = stringDate.substring(0,2);
var month = stringDate.substring(2,4);
var day = stringDate.substring(4,6);
var d1;
if (day==="00")
{
d1 = new Date(month + '/01/20' + year);
d1.setDate(d1.getDate() -1);
//console.log("day1" + d1);
}
else
{
d1 = new Date('20' + year, month, day);
}
var formattedDate = d1.getMonth() + '/' + d1.getDate() + '/' + d1.getFullYear();
console.log(formattedDate);
}

JavaScript date always returns "2015-11-1" instead of today's date

var year = new Date().getFullYear();
var month = new Date().getMonth();
var day = new Date().getDay();
var currDate = year + "-" + month + "-" + day;
New Date() will return today's date. Above code should return today's date like "2015-12-18", but it returns "2015-11-1". Anyone knows why? Thanks.
Months are zero based, so you always have to add 1 to get the correct month.
var month = new Date().getMonth() + 1;
or subtract 1 if setting it
getDay() is just the wrong method, it gets the day of the week, 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on, not the date, that would be
var day = new Date().getDate();
http://www.w3schools.com/js/js_date_methods.asp
Date.getDay() returns the day in the week (1 for monday - 0 to 6) so you need Date.getDate()
Date.getMonth() returns the month (0-11) so you need +1

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()

JavaScript date: How to add a year to a string that contains mm/dd?

I have an string that contains month/date and I need to insert the year. The string looks like:
Last Mark:: 2/27 6:57 PM
I want to convert the string to something like:
Last Mark:: 2010/02/27 18:57
In this case, there will not be any entries more than a year old. For example, if the date were 10/12 it can be assumed that the year is 2009.
What is the best method for this?
Following from Adam's suggestion:
function convertDate(yourDate) {
var today = new Date();
var newDate = new Date(today.getFullYear() + '/' + yourDate);
// If newDate is in the future, subtract 1 from year
if (newDate > today)
newDate.setFullYear(newDate.getFullYear() - 1);
// Get the month and day value from newDate
var month = newDate.getMonth() + 1;
var day = newDate.getDate();
// Add the 0 padding to months and days smaller than 10
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
// Return a string in YYYY/MM/DD HH:MM format
return newDate.getFullYear() + '/' +
month + '/' +
day + ' ' +
newDate.getHours() + ':' +
newDate.getMinutes();
}
convertDate('2/27 6:57 PM'); // Returns: "2010/02/27 18:57"
convertDate('3/27 6:57 PM'); // Returns: "2009/03/27 18:57"
the code for adding THIS year is simple
var d = Date();
var withYear = d.getFullYear() + yourDate;
however, the logic behind considerating if it should be this year or last year could be harder to do
I would think this way: get today's date. If the date is higher than today's, it's last year, so add d.getFullYear()-1, otherwise add d.getFullYear()
This returns the current year:
var d = new Date();
var year = d.getFullYear();
To figure whether its this year or not you could just compare the day and month with the current day and month, and if necessary, subtract 1 from the year.
To get the day and month from the Date object:
d.getMonth(); // warning this is 0-indexed (0-11)
d.getDate(); // this is 1-indexed (1-31)

Categories

Resources