我需要显示一个日期的名称(如“05/23/2014”),这是我从第三方获得的。
我已尝试使用Date
,但我只收到日期。
获取当天名称的正确方法是什么?
答案 0 :(得分:88)
Ahum,三年后......
为什么没有人使用标准javascript Date类提供的方法(Callum Linington除外)?
从日期获取日期名称:
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.
将所有工作日纳入数组:
function getWeekDays(locale)
{
var baseDate = new Date(Date.UTC(2017, 0, 2)); // just a Monday
var weekDays = [];
for(i = 0; i < 7; i++)
{
weekDays.push(baseDate.toLocaleDateString(locale, { weekday: 'long' }));
baseDate.setDate(baseDate.getDate() + 1);
}
return weekDays;
}
var weekDays = getWeekDays('nl-NL'); // Gives back { 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag', 'zondag'} which are the days of the week in Dutch.
对于美国日期,请使用“en-US”作为区域设置。
答案 1 :(得分:64)
您可以使用Date.getDay()
方法,该方法在星期日返回0,在星期六返回6。因此,您只需创建一个包含日期名称名称的数组:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var d = new Date(dateString);
var dayName = days[d.getDay()];
此处dateString
是您从第三方API收到的字符串。
或者,如果您想要日期名称的前3个字母,则可以使用Date
对象的内置toString
方法:
var d = new Date(dateString);
var dayName = d.toString().split(' ')[0];
这将取d.toString()
输出中的第一个单词,即3个字母的日期名称。
答案 2 :(得分:9)
let weekday = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][new Date().getDay()]
答案 3 :(得分:6)
var days = [
"Sunday",
"Monday",
"...", //etc
"Saturday"
];
console.log(days[new Date().getDay()]);
简单,请阅读JavaScript手册中的Date对象
要使用日期做其他事情,比如从中获取可读字符串,我使用:
var d = new Date();
d.toLocaleString();
如果您只想使用时间或日期:
d.toLocaleTimeString();
d.toLocaleDateString();
您可以通过执行以下操作来解析日期:
var d = new Date(dateToParse);
或
var d = Date.parse(dateToParse);
答案 4 :(得分:4)
let weekday = new Date(dateString).toLocaleString('en-us', {weekday:'long'});
console.log('Weekday',weekday);
答案 5 :(得分:4)
使用Date.toLocaleString()方法:
new Date(dateString).toLocaleString('en-us', {weekday:'long'})
答案 6 :(得分:3)
要从任何给定日期获取日期,只需将日期传递给新的 Date 对象:
let date = new Date("01/05/2020");
let day = date.toLocaleString('en-us', {weekday: 'long'});
console.log(day);
// expected result = tuesday
要了解更多信息,请转到 mdn-date.prototype.toLocaleString()(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString)
答案 7 :(得分:3)
最简单的方法:
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var dayName = days[new Date().getDay()];
答案 8 :(得分:1)
你可以使用一个对象
var days = {
'Mon': 'Monday',
'etc..': 'etc..',
'Fri': 'Friday'
}
var date = new Date().toString().split(' ')[0]; //get day abreviation first
console.log(days[date]);
答案 9 :(得分:1)
如果其他人提出更好的建议,我不喜欢过于复杂的解决方案,请告诉我们:)
any-name.js
var today = new Date().toLocaleDateString(undefined, {
day: '2-digit',
month: '2-digit',
year: 'numeric',
weekday: 'long'
});
any-name.html
<script>
document.write(today);
</script>
答案 10 :(得分:1)
第一解决方案
var today = new Date();
var day = today.getDay();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var dayname = days[day];
document.write(dayname);
解决方案2
var today = new Date();
var day = today.getDay();
switch(day){
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day ="Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thrusday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
}
document.write(day);
答案 11 :(得分:1)
最短的一支班轮
如果您希望阵列从星期日开始,则将UTC日期从6更改为5。
const getWeekDays = (locale) => [...Array(7).keys()].map((v)=>new Date(Date.UTC(1970, 0, 6+v)).toLocaleDateString(locale, { weekday: 'long' }));
console.log(getWeekDays('de-DE'));
答案 12 :(得分:0)
location /i/ {
alias /data/w3/images/;
}
答案 13 :(得分:0)
另一种选择是使用内置函数Intl.DateTimeFormat
,例如:
function getDayName(dateString) {
const [date, options] = [new Date(dateString), {weekday: 'long'}];
return new Intl.DateTimeFormat('en-Us', options).format(date);
}
<label for="inp">Enter a date string in the format "MM/DD/YYYY" or "YYYY-MM-DD" and press "OK":</label><br>
<input type="text" id="inp" value="01/31/2021">
<button onclick="alert(getDayName(document.getElementById('inp').value))">OK</button>
答案 14 :(得分:0)
单行解决方案:
const day = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"][new Date().getDay()]
答案 15 :(得分:0)
只需使用它即可:
function getWeekDayNames(format = 'short', locale = 'ru') {
const names = [];
const date = new Date('2020-05-24');
let days = 7;
while (days !== 0) {
date.setDate(date.getDate() + 1);
names.push(date.toLocaleDateString(locale, { weekday: format }));
days--;
}
return names;
}
关于您可以在此处阅读的格式Documentation DateTimeFormat
答案 16 :(得分:0)
看看这个:
var event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(event.toLocaleDateString('de-DE', options));
// expected output: Donnerstag, 20. Dezember 2012
console.log(event.toLocaleDateString('ar-EG', options));
// expected output: الخميس، ٢٠ ديسمبر، ٢٠١٢
console.log(event.toLocaleDateString('ko-KR', options));
// expected output: 2012년 12월 20일 목요일
来源:Mozilla Doc
答案 17 :(得分:0)
尝试使用此代码:
var event = new Date();
var options = { weekday: 'long' };
console.log(event.toLocaleDateString('en-US', options));
这将为您提供字符串格式的日期名称。
答案 18 :(得分:-1)
不是最好的方法,而是使用数组。这只是一种替代方法。
http://www.w3schools.com/jsref/jsref_getday.asp
var date = new Date();
var day = date.getDay();
在发布此处之前,您应该真的使用谷歌。
由于其他人发布了数组方法,我将使用switch语句向您展示另一种方法。
switch(day) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
... rest of cases
default:
// do something
break;
}
上述工作,然而,阵列是更好的选择。您也可以使用if()
语句,但是如果有一些语句会比几个语句更清晰。
答案 19 :(得分:-2)
let dateString = '5/23/2014'
// converting Date String to Javascript Date Format
let day = new Date(dateString).getDay();
let month = new Date(dateString).getMonth()
let year = new Date(dateString).getFullYear()
let dayName;
if (day ==0){
dayName= 'Sunday'
}else if (day == 1){
dayName= 'Monday'
}else if (day == 2){
dayName= 'Tuesday'
}else if (day == 3){
dayName= 'Wednesday'
}else if (day == 4){
dayName= 'Thursday'
}else if (day ==5){
dayName= 'Friday'
}else {
dayName= 'Saturday'
}
console.log(`Day : ${dayName} Month: ${month+1} Year : ${year}`)
答案 20 :(得分:-3)
var day ;
switch(new Date().getDay()){
case 0:
day="Sunday";
break;
case 1:
day="Monday";
break;
case 2:
day="Tuesday";
break;
case 3:
day="wednesday";
break;
case 4:
day ="Thurs day"
break;
case 5:
day ="friday";
break;
case 6:
day ="Satureday";
break;
}
document.write(day);