简体   繁体   English

从日期中获取日期名称,格式为 dd-mm-yyyy?

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

I need a way of getting the name of the day eg Monday, Tuesday from a date with the format of DD-MM-YYYY I 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.我需要一种从日期格式为 DD-MM-YYYY 的日期获取日期的方法,例如星期一、星期二YYYY,我不能使用getDay()因为格式不同意。

I also can't use new Date() because i has to be a date selected from a calendar.我也不能使用new Date()因为我必须是从日历中选择的日期。 Not todays date.不是今天的日期。 When I run the following code I get the error:当我运行以下代码时,出现错误:

date.getDay() is not a function. date.getDay()不是函数。

$('#datepicker').datetimepicker().on('dp.change', function (event) {
        let date = $(this).val();
        let day = date.getDay();
        console.log(day);
      });
```

Anyone any ideas?

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: 强烈不鼓励通过Date构造函数按原样解析字符串,因此我宁愿建议通过以下方式将日期字符串转换为Date

 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))

 function get_day(date){ let d=new Date(date); let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; let day_index=d.getDay(); return days[day_index] } let today=new Date(); console.log("today is",get_day(today))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM