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()
Related
How can I get the day that the next month starts on? (Monday Tuesday etc.).
I tried the following but it returns more than just the day.
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
You're almost there, just add this after your last line of code to get the name of the day
var dayName = lastDay.toLocaleDateString('en-us', { weekday: 'long' });
Also keep in mind that Date constructor's second parameter is monthIndex MDN.
You should add m by 1 to get next month's first day and add it by 2 and provide date = 0 to get the last day of the next month.
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m + 1, 1);
var lastDay = new Date(y, m + 2, 0);
var firstDayName = firstDay .toLocaleDateString('en-us', { weekday: 'long' });
var lastDayName = lastDay.toLocaleDateString('en-us', { weekday: 'long' });
console.log(firstDayName, lastDayName)
Your current code is getting Date instances for the first and last day of the current month. So first you'll need to fix that. Then, if you want day numbers instead, you have to call getDay to get the values (0 = Sunday through 6 = Saturday):
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1; // + 1 = next month
const firstDay = new Date(year, month, 1).getDay();
const lastDay = new Date(year, month + 1, 0).getDay();
console.log({firstDay, lastDay});
As I write this on July 31st, that shows 1 and 3, telling us that August 2022 starts on Monday and ends on Wednesday.
I just took the current date and got the first day of the month. Then we just converted this to string. It's working. Try this:
const date = new Date(); // current date 1/8/2022
const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 1); //first day of next month
const nextMonthStartDay = nextMonth.toLocaleDateString('en-US', { weekday: 'long' }); //convert to date string
console.log(nextMonthStartDay) //Thursday
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
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
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/
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)