Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript - get the day name from a date

I tried to get the day name from a date, but with the date 27/02/2021 i get Invalid Date

new Date(exam.examDate).toLocaleString('fr-FR', { weekday: 'long' })

i tried to convert the date using datePipe but it return the below error

Unable to convert "27/02/2021" into a date' for pipe 'DatePipe

new Date(this.datePipe.transform(exam.examDate, "dd/MM/yyyy")).toLocaleString('fr-FR', { weekday: 'long' })
like image 931
Aymen Kanzari Avatar asked Feb 17 '21 20:02

Aymen Kanzari


People also ask

How do I get the day name from date in react?

Getting the day name from a date: function getDayName(dateStr, locale) { var date = new Date(dateStr); return date. toLocaleDateString(locale, { weekday: 'long' }); } var dateStr = '05/23/2014'; var day = getDayName(dateStr, "nl-NL"); // Gives back 'Vrijdag' which is Dutch for Friday.

What is the keyword for extracting the day of the month JavaScript?

Using the getUTCDate() Method All JavaScript getUTC() methods presumptively use local time for the Date. This function returns the date object's day of the month, ranging from 1 to 31. The day according to UTC, is returned by the getUTCDate() function.

How do I find the weekly date from a name?

getDay() getDay() is an date method in JavaScript, which is used to return the day of the week (Name/number of the day in a week between 0 to 6) for the specified date. As we all know there are 7 days in a week, the numbering for those days in JavaScript goes as follow, Sunday=0, Monday=1, Tuesday=2,................


3 Answers

You can get a custom day name from Date like:

const names = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const dayName = names[dateObject.getDay()];

const date = '27/02/2021';
const d = date.split('/');
const names = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const dayName = names[new Date(d[2],d[1],d[0]).getDay()];
console.log(dayName);
like image 133
Garto Avatar answered Oct 17 '22 06:10

Garto


examDate = "27/02/2021";
date = examDate.split("/");
x = new Date(date[2], date[1] - 1, date[0]);

// it prints "samedi"
console.log(x.toLocaleString('fr-FR', { weekday: 'long' }));
like image 40
Saad Zimat Avatar answered Oct 17 '22 05:10

Saad Zimat


This is because Date expects MM/DD/YYYY but you're passing DD/MM/YYYY:

const getDay = date => {
  const parts = date.split('/');
  return new Date(+parts[2], +parts[1] - 1, +parts[0]).toString().split(' ')[0]; 
}

console.log( getDay('27/02/2021') );

Using moment.js:

const getDay = date =>
  moment(date, "DD/MM/YYYY").format('ddd');

console.log( getDay('27/02/2021') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
like image 1
Majed Badawi Avatar answered Oct 17 '22 04:10

Majed Badawi