Day Name from Date in Js

Day Name from Date in JS

You could use the Date.getDay() method, which returns 0 for sunday, up to 6 for saturday. So, you could simply create an array with the name for the day names:

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var d = new Date(dateString);
var dayName = days[d.getDay()];

Here dateString is the string you received from the third party API.

Alternatively, if you want the first 3 letters of the day name, you could use the Date object's built-in toString method:

var d = new Date(dateString);
var dayName = d.toString().split(' ')[0];

That will take the first word in the d.toString() output, which will be the 3-letter day name.

How to get day name in a date in node js

You need to include weekday: 'long' in the options object:

var date = new Date();console.log(date.toLocaleString('en-US', {      weekday: 'long',      year: 'numeric',      month: 'long',      day: 'numeric'    }));

How to get day name with date in nodejs?

new Date('12/27/2019').getDay() you need to swap day with month.

How to Get Only Day Name or Number in a Week in JavaScript Date()

Use haven't use getDay

today.getDay();

first of month

 firstOfMonth.getDay();

last of month

 lastOfMonth.getDay();

or if u want to get dayname use

var days = ['Sun','Mon','Tues','Wed','Thrus','Fri','Sat'];
//First of month
days[firstOfMonth.getDay()];
//last of month
days[lastOfMonth.getDay()];

Get day name from date with format dd-mm-yyyy?

Parsing string as-is by Date constructor is strongly discouraged, so I would rather recommend to convert your date string into Date the following way:

const dateStr = '15-09-2020',

getWeekday = s => {
const [dd, mm, yyyy] = s.split('-'),
date = new Date(yyyy, mm-1, dd)
return date.toLocaleDateString('en-US', {weekday: 'long'})
}

console.log(getWeekday(dateStr))

How to get the day name from the date string regardless of user's timezone?

You can use date-fns-tz as suggested by the other comment. It interally uses Intl. If you only care about getting the day you can just use Intl directly:

let formatter = new Intl.DateTimeFormat('en-US', {
timeZone: 'UTC',
weekday: 'long',
});

formatter.format(new Date("2021-08-11")) // outputs: "Wednesday"

I would recommend that you also format your dates to ISO i.e. "2021-08-11T00:00:00.000Z" as the Z at the end will ensure that the date is parsed in UTC before formatting.

how to take out the day name from the input date without using moment js in react

I think it is better practice to use toLocaleString when you work with dates in javscript, for example, if you will try to use array where you have week days and get the specific string from that by your date format is bad, because on IOS you will get undefined because "12/01/1994" is not a format supported by ECMA-262 so parsing implementation will be dependent and iOS will treat it as an invalid date.

const getWeekday = (dateFormat) => {
// split date in non-digit chaarcters
let [d, m, y] = dateFormat.split(/\D/);

//put them in Date method
const date = new Date(y, m - 1, d)
//and return weekday in long format
const weekday = date.toLocaleString("default", { weekday: "long" })

return weekday
}

console.log(getWeekday('12/01/1994'))


Related Topics



Leave a reply



Submit