首页 > 解决方案 > JS。从字符串中获取部分日期

问题描述

我有这个字符串 '2020-10-20' 我可以像这样分别得到这些数字吗:

const [year, month, day] = '2020-10-20'.match(/\d\d\d\d/ what should i write there?)

还是存在更好的方法?

标签: javascriptstringdatesplit

解决方案


您可以使用分隔符拆分字符串,如下所示

const [year, month, day] = '2020-10-20'.split("-")

那么你会有year=2020等。


推荐阅读