[英]How to create a function that returns the number of days to the next specified weekday js?
Write a function that accepts a weekday as a string (eg 'Sunday') and returns the number of days to the next specified weekday.编写一个 function 接受工作日作为字符串(例如“星期日”)并返回下一个指定工作日的天数。 The input should be case-insensitive.输入应该不区分大小写。 If the specified weekday is today, return Hey, today is ${ specifiedWeekday } =)
, otherwise return It's ${ number } day(s) left till ${ specifiedWeekday }
.如果指定的工作日是今天,返回Hey, today is ${ specifiedWeekday } =)
,否则返回It's ${ number } day(s) left till ${ specifiedWeekday }
。 Please note, although input is case-insensitive, weekday name in the output string should be always in proper case.请注意,虽然输入不区分大小写,但 output 字符串中的工作日名称应始终采用正确的大小写。 howFarIs('friday'); howFarIs('星期五'); // "It's 1 day(s) left till Friday." // “距离星期五还有 1 天。” (on October 22nd) howFarIs('Thursday'); (10 月 22 日)howFarIs('Thursday'); // "Hey, today is Thursday =)" (on October 22nd) // “嘿,今天是星期四 =)”(10 月 22 日)
That s what I
we done, pls help me with the next steps or with the correct solution.这s what I
,请帮助我完成后续步骤或提供正确的解决方案。
function howFarIs(day) {
let test = new RegExp(`${day}`, 'gi')
for (let day of days) {
if (day.match(test)) {
var dayIndex = days.indexOf(day);
}
}
let date = new Date();
let todayIndex = date.getDay()
}
I don't think a RegExp is necessary for this one.我认为这个不需要 RegExp。 You should have most of the code below.您应该拥有下面的大部分代码。 It's still needs a few adjustments here and there but most of the logic is there.它仍然需要在这里和那里进行一些调整,但大部分逻辑都在那里。
const dayLC = day.toLowerCase();
const daysOfTheWeek = ['sunday','monday','tuesday','etc'];
const index = daysOfTheWeek.findIndex((e) => e === dayLC);
const today = (new Date()).getDay();
if(today===index)
{
return `Hey, today is ${capitalize(daysOfTheWeek[today])}`;
}
else if(today<index)
{
return `It's ${ index-today } day(s) left till ${ specifiedWeekday }`
else
{
//index < today
return `It's ${ index-today+7 } day(s) left till ${ specifiedWeekday }`
function capitalize(word) {
const lower = word.toLowerCase();
return lower.charAt(0).toUpperCase() + lower.slice(1);
}
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
function howFarIs(dayOfWeek) {
let test = new RegExp(`${dayOfWeek}`, 'gi')
for (let day of days) {
if (day.match(test)) {
var dayIndex = days.indexOf(day);
}
}
let dayMilliseconds =
1000 * 60 * 60 * 24;
var resultDate = new Date(new Date().getTime());
resultDate.setDate(new Date().getDate() + (7 + dayIndex - new Date().getDay()) % 7);
var diff = Math.abs((new Date().getTime() - resultDate.getTime()) / (dayMilliseconds));
if (diff == 0) {
var result = `Hey, today is ${days[dayIndex]} =)`
} else {
result = `It's ${diff} day(s) left till ${days[dayIndex]}.`
}
return result;
}
const days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
function howFarIs(day) {
const day_index = days_of_week.findIndex(weekday => {
return weekday.toLowerCase() === day.toLowerCase();
});
if (day_index === -1) {
return 'Invalid parameter entered.';
}
let today = new Date();
let todayIndex = today.getDay();
if (todayIndex === day_index) {
return `Hey, today is ${days_of_week[day_index]}`;
} else if (todayIndex > day_index) {
var number = day_index + (7-todayIndex);
} else {
var number = day_index - todayIndex;
}
return `${ number } day(s) left till ${ days_of_week[day_index] }`;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.