Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
25668ac5
authored
Mar 22, 2023
by
puhui999
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: 迁移v2版本的日期格式化函数到vue3中
parent
bf2b4ced
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
0 deletions
+45
-0
src/utils/formatTime.ts
+45
-0
No files found.
src/utils/formatTime.ts
View file @
25668ac5
...
...
@@ -64,6 +64,51 @@ export function formatDate(date: Date, format: string): string {
}
return
format
}
// 日期格式化
export
function
parseTime
(
time
:
any
,
pattern
?:
string
)
{
if
(
arguments
.
length
===
0
||
!
time
)
{
return
null
}
const
format
=
pattern
||
'{y}-{m}-{d} {h}:{i}:{s}'
let
date
if
(
typeof
time
===
'object'
)
{
date
=
time
}
else
{
if
(
typeof
time
===
'string'
&&
/^
[
0-9
]
+$/
.
test
(
time
))
{
time
=
parseInt
(
time
)
}
else
if
(
typeof
time
===
'string'
)
{
time
=
time
.
replace
(
new
RegExp
(
/-/gm
),
'/'
)
.
replace
(
'T'
,
' '
)
.
replace
(
new
RegExp
(
/
\.\d{3}
/gm
),
''
)
}
if
(
typeof
time
===
'number'
&&
time
.
toString
().
length
===
10
)
{
time
=
time
*
1000
}
date
=
new
Date
(
time
)
}
const
formatObj
=
{
y
:
date
.
getFullYear
(),
m
:
date
.
getMonth
()
+
1
,
d
:
date
.
getDate
(),
h
:
date
.
getHours
(),
i
:
date
.
getMinutes
(),
s
:
date
.
getSeconds
(),
a
:
date
.
getDay
()
}
const
time_str
=
format
.
replace
(
/{
([
ymdhisa
])
+}/g
,
(
result
,
key
)
=>
{
let
value
=
formatObj
[
key
]
// Note: getDay() returns 0 on Sunday
if
(
key
===
'a'
)
{
return
[
'日'
,
'一'
,
'二'
,
'三'
,
'四'
,
'五'
,
'六'
][
value
]
}
if
(
result
.
length
>
0
&&
value
<
10
)
{
value
=
'0'
+
value
}
return
value
||
0
})
return
time_str
}
/**
* 获取当前日期是第几周
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment