wwjdm
wwjdm

Reputation: 2596

golang: Convert date with time to seconds

If I have date format: "1/_2/2006, 15:04:05"

How to I convert the entire date to seconds. Is there a golang time method?

Upvotes: 1

Views: 19314

Answers (2)

evanmcdonnal
evanmcdonnal

Reputation: 48076

There's a nice little package on GitHub to abstract the format matching part of Parse (it requires you provide a string to explain the format of your date); https://github.com/araddon/dateparse

You can just use ParseAny to get a time.Time{} call it ts then ts.Unix to get seconds. I would recommend it since Parse itself is extremely sensitive to it's input.

Upvotes: 1

Evan
Evan

Reputation: 6545

You can use time.Parse and then call Unix on the result:

Upvotes: 9

Related Questions