Js 以字符串形式返回开始日期和结束日期
我有这样的字符串:
(Feb 28-Mar 1)
(Mar 2-3)
我希望我返回一个对象,如下所示,有人给出一些建议我该怎么做?
function rD(text){
let date = text.replace('(', '').replace(')', '').split(' ');
//const [start, end] = date[2].split("-").map(Number);
return date;
}
console.log(rD("(Feb 28-Mar 1)"))
console.log(rD("(Mar 2-3)"))
返回:
[
{
month: 2,
day: 28
},
{
month: 3,
day: 1
}
]
[
{
month: 3,
day: 2
},
{
month: 3,
day: 3
}
]
I have strings like this:
(Feb 28-Mar 1)
(Mar 2-3)
I would like me to return an object as you can see below, someone to give some suggestion how can I do?
function rD(text){
let date = text.replace('(', '').replace(')', '').split(' ');
//const [start, end] = date[2].split("-").map(Number);
return date;
}
console.log(rD("(Feb 28-Mar 1)"))
console.log(rD("(Mar 2-3)"))
Return:
[
{
month: 2,
day: 28
},
{
month: 3,
day: 1
}
]
[
{
month: 3,
day: 2
},
{
month: 3,
day: 3
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议使用正则表达式模式来解析每个范围。
由此我们可以得到startMonth、startDay、endMonth、endDay。然后,我们可以创建一个
getMonthNumber()
函数,将月份名称缩写(Jan、Feb 等)转换为数字。I'd suggest using a regex pattern to parse each span.
From this we can get the startMonth, startDay, endMonth, endDay. We can then create a
getMonthNumber()
function to turn the abbreviated month name (Jan, Feb, etc.) to a number.我会先删除括号,然后用
/[ -]/
分隔。这样您就可以得到这两种形式之一的数组,或者
现在,如果数组有 4 个元素,则第一个和第三个元素始终是月份,第二个和第四个元素始终是日期。如果数组有 3 个元素,第一个元素是月份(开始和结束),第二个和第三个元素是日期。
要获取月份的数字,您可以进行简单的查找,例如
I'd remove the parentheses first and then split by
/[ -]/
. This way you get an array in one of these two formsor
Now if the array has 4 elements the first and third are always the month and second and forth are the day. If the array has 3 elements, the first is a month for both, start and end, the second and third are the days.
For getting the number of the month you can have a simple lookup like
你可以试试这个
You can try this
首先,我们将为几个月创建一个映射器。像这样:
然后我们需要一个函数,通过删除括号并用连字符将其分割来将字符串切割成块。
第一个块是开始日期和结束日期。
有了这两个日期,我们可以进一步得到开始月份、开始日期、结束月份和结束日期。 (通过用空格分割我们的块)
从您的示例中我只能看到一种特殊情况,即结束日期未指定月份的情况,在这种情况下,它隐式是开始月份。
First we are going to create a mapper for the months. like this:
Then we need a function which cutes the string into chunks by removing the parenthesis and splitting it by its hyphen.
The first chunks are the start and end dates.
With these two dates we can further get the start month, start day, end month, and end day. (By splitting our chunks by there whitespaces)
There is only one special case I can see from your example and that is the case when the end date does not specify a month, in which case it is implicitly the start month.
除了代码之外,这种方法的优点是,如果输入类似于
Jan 1-3-Mar 7
,它就可以工作,而所有其他答案都没有考虑到这一点,因此返回 <代码>未定义或错误。简单的正则表达式和月份到数字的列表应该可以工作,这对我来说似乎是最简单的。使用 String.prototype.match 我们可以“忽略”所有额外的数据(即月份之间的括号和破折号),只需提取必要的数据:
Besides the code, the advantage to this approach is that it works if the input is something like
Jan 1-3-Mar 7
, while all the other answers don't put this into consideration, thus returningundefined
or an error.Simple regex and a list of month to numbers should work, and it seems the most straightforward to me. Using
String.prototype.match
we can "ignore" all the extra data (i.e. the parentheses and dashes between months), just extract the necessary data:在下面的代码片段中添加了内联的所有步骤。
Added all the steps inline in below code snippet.