admin管理员组文章数量:1395451
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' })
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' })
Share
Improve this question
asked Feb 17, 2021 at 20:32
Aymen KanzariAymen Kanzari
2,0339 gold badges51 silver badges91 bronze badges
3 Answers
Reset to default 2This 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./ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
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);
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' }));
本文标签: javascripttypescriptget the day name from a dateStack Overflow
版权声明:本文标题:javascript - typescript - get the day name from a date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744678967a2619282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论