static
func
getNowTimeStamp
() ->
Int
{
let
nowDate
=
Date
.
init
()
let
interval
=
Int
(nowDate.timeIntervalSince1970)
return
interval
static
func
getNowTimeString
(
dateFormat
:
String
) ->
String
{
let
dateformatter
=
DateFormatter
()
dateformatter.dateFormat
=
dateFormat
let
nowDate
=
Date
.
init
()
return
dateformatter.string(from: nowDate)
static
func
getTimeString
(
timeStamp
:
Int
,
dateFormat
:
String
) ->
String
{
let
date
=
Date
(timeIntervalSince1970:
TimeInterval
.
init
(timeStamp))
let
dateformatter
=
DateFormatter
()
dateformatter.dateFormat
=
dateFormat
return
dateformatter.string(from: date)
static
func
getDate
(
timeString
:
String
,
dateFormat
:
String
) ->
Date
{
let
dateformatter
=
DateFormatter
()
dateformatter.dateFormat
=
dateFormat
let
date
=
dateformatter.date(from: timeString)
??
Date
()
return
date
static
func
getTimeStamp
(
timeString
:
String
,
dateFormat
:
String
) ->
Int
{
let
dateformatter
=
DateFormatter
()
dateformatter.dateFormat
=
dateFormat
let
date
=
self
.getDate(timeString: timeString, dateFormat: dateFormat)
return
Int
(date.timeIntervalSince1970)
static
func
getDateWith
(
timeStamp
:
Int
) ->
Date
{
let
date
=
Date
(timeIntervalSince1970:
TimeInterval
.
init
(timeStamp))
return
date
func
getTime
() -> (
String
,
String
,
String
,
String
,
String
,
String
) {
let
dateformatter
=
DateFormatter
()
dateformatter.dateFormat
=
"yyyy"
let
y
=
dateformatter.string(from:
self
)
dateformatter.dateFormat
=
"MM"
let
mo
=
dateformatter.string(from:
self
)
dateformatter.dateFormat
=
"dd"
let
d
=
dateformatter.string(from:
self
)
dateformatter.dateFormat
=
"HH"
let
h
=
dateformatter.string(from:
self
)
dateformatter.dateFormat
=
"mm"
let
m
=
dateformatter.string(from:
self
)
dateformatter.dateFormat
=
"ss"
let
s
=
dateformatter.string(from:
self
)
return
(y, mo, d, h, m, s)
func
getStringTime
(
dateFormat
:
String
) ->
String
{
let
dateformatter
=
DateFormatter
()
dateformatter.dateFormat
=
dateFormat
return
dateformatter.string(from:
self
)
demo参考:
github