How do i print only the date of the week with JS. Or maybe the day and month?
I was playing around with some functions but I only get the whole date.
let today = new Date()
today.getDate()
let tomorrow = new Date();
tomorrow.getDay(today.getDate()+1)
Then I will have to use the variable as props in a react ponent and it should be a string. So should I use toUTCstring?
How do i print only the date of the week with JS. Or maybe the day and month?
I was playing around with some functions but I only get the whole date.
let today = new Date()
today.getDate()
let tomorrow = new Date();
tomorrow.getDay(today.getDate()+1)
Then I will have to use the variable as props in a react ponent and it should be a string. So should I use toUTCstring?
Share Improve this question asked May 5, 2019 at 14:33 StranykaStranyka 1413 silver badges12 bronze badges 3- What's "date of the week", do you want the day, as in "sunday", and month as "may" etc ? – adeneo Commented May 5, 2019 at 14:35
- Yes, either the day in number or words. I know I can do that with toLocaleString() to but I don't know how to get rid of the time. – Stranyka Commented May 5, 2019 at 14:38
-
Did you try
getDay
– adeneo Commented May 5, 2019 at 14:41
8 Answers
Reset to default 2You can use the weekday
option of toLocaleString
:
mydateobject.toLocaleString("en", { weekday: "long" })
The month name can be got in a similar way:
mydateobject.toLocaleString("en", { month: "long" })
getDay
takes no arguments. You're looking for setDate
to set the date to tomorrow, and then use getDay
without arguments:
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
let days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
console.log('tomorrow is: ' + days[tomorrow.getDay()])
You can use the getDay()
method. This will return a number. You can use this number to get the name of the day. Something like this
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
let date = new Date();
var day_of_week = date.getDay();
var day_name = days[day_of_week];
the javascript Date object has many methods to get what you want: you can use getDay() to get the day of the week and getMonth() to get the month.
To pass this as a string parameter I believe the best would be to use toISOString() and you can use this string as a parameter to the Date constructor on the other side.
For handling dates I remend using moment js library
you can install it with npm install moment
and then
moment().format('E');
or
moment().utc.().format('E');
or just moment().day()
Looks like toDateString
will always return the 3 letters of the day if the week, so you can:
// Today
new Date().toDateString().substring(0,3)
// Tomorrow
new Date(Date.now()+24*3600*1e3).toDateString().substring(0,3)
Or, to get the number (zero based):
// Today
new Date().getDay()
// Tomorrow
new Date(Date.now()+24*3600*1e3).getDay()
If you use momentjs library, you can use let day = moment(tomorrow).format('dddd');
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(tomorrow)
let day = moment(tomorrow).format('dddd');
console.log(day);
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(tomorrow)
let day = moment(tomorrow).format('dddd');
console.log(day);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.24.0/moment.js"></script>
Using Date.toDateString() you can format into dd mmm yy (or any way you want):
let date = new Date
str = date.toDateString() // 'Mon May 06 2019
let arr = str.split(" ") // ['Mon', 'May', '06', '2019']
let formatted = parseInt(arr[2]).toString() + " " + arr[1] + " " + arr[3] //6 May 2019
console.log(formatted) //6 May 2019
Using Date.toLocaleString() gives weekday:
let date = new Date
let weekdayLong = date.toLocaleString('en-gb', {weekday: 'long'}) //Monday
let weekdayShort = date.toLocaleString('en-gb', {weekday: 'short'}) //Mon