问题描述
我需要一种获取日期名称的方法,例如从DD-MM-YYYY格式的日期开始我正在使用引导程序datetimepicker,并且当我选择一个日期时,该值仅采用DD-MM-YYYY格式,因此我无法使用 getDay()
,因为格式与之不一致.
I need a way of getting the name of the day e.g Monday, Tuesday from a date with the format of DD-MM-YYYYI am using bootstrap datetimepicker and when i select a date, the value is just in the format DD-MM-YYYY, I can't use getDay()
because the format doesn't agree with it.
我也不能使用 new Date()
,因为我必须是从日历中选择的日期.不是今天.当我运行以下代码时,出现错误:
I also can't use new Date()
because i has to be a date selected from a calendar. Not todays date.When I run the following code I get the error:
date.getDay()
不是函数.
$('#datepicker').datetimepicker().on('dp.change', function (event) {
let date = $(this).val();
let day = date.getDay();
console.log(day);
});
```
Anyone any ideas?
推荐答案
按 Date
构造函数按原样解析字符串 强烈建议 ,因此,我建议您通过以下方式将日期字符串转换为 Date
:
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))
这篇关于从日期获取日期名称,格式为dd-mm-yyyy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!