Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化 parseTime #1546

Merged
merged 2 commits into from
Jan 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export function parseTime(time, cFormat) {
if (typeof time === 'object') {
date = time
} else {
if (('' + time).length === 10) time = parseInt(time) * 1000
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date = new Date(+time)

就可以了吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date = new Date(+time) 会有bug,比如传入的是"01/23/2019"或"2019-01-23"

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但还有一个问题 if (time.toString().length === 10) time = time * 1000
if 放的位置不太对吧,这样会导致 十位的number类型时间戳不能正确转化了。

parseTime(1548221490)
//"1970-01-19 06:03:41"
parseTime("1548221490")
//"2019-01-23 13:31:30"

}
const formatObj = {
Expand Down