Converting human-readable time back into a Unix string?

These are my steps:
1 - Get Unix date from db
2 - Get browser timezone
3 - Convert unix time into timezone time
4 - Format into “MMM DD, YYYY @ HH:MM:SS”

My biggest issue is step 3. When I use toLocaleString("en-US", {timeZone: tz}) (timezone being previously defined), the time string is converted into a non-Unix string: "Sat Sep 05 2020 21:44:53 GMT-0700"
Is there any way I can back-convert this into a UNIX string? I can’t chain together all of the manipulations… any help would be appreciated!

Try a lib such as Momentjs.

var moment = require("moment")
moment(TimeInUNIX).format("ddd MMM Do YYYY h:mm:ss a")

Keep in mind that this is rough code and could use some tweeking.

Ah, I actually wanted to change a human-readable time into Unix time. Is this possible in Moment?

Hi @CarlyRaeJepsenStan!
Just use @Anish’s code to do everything you need and convert the Unix from the DB to the local timezone, not just convert from Date to Unix timestamp

1 Like

I did a little bit more research and i found this:
It comes with moment.format() pre-installed which is nice.

Could work client-side with unpkg and a little magic

Finally got it to work!

var moment = require("moment-timezone")
var june = moment(1599451463030).format();
var asdf = moment(june.toString())'America/Los_Angeles').format('ha z')
var moment = require("moment-timezone")
var june = moment(1599451463030).tz('America/Los_Angeles').format('ha z')

This should work even better.

1 Like