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

Add fromUnixTime function #830

Closed
envil opened this issue Aug 7, 2018 · 10 comments
Closed

Add fromUnixTime function #830

envil opened this issue Aug 7, 2018 · 10 comments

Comments

@envil
Copy link

envil commented Aug 7, 2018

I tried to find the function similar to moment.unix() where I want to create a date from an unix timestamp in date-fns but couldn't find it. Is it me couldn't find it or it's indeed not exist?

@envil envil changed the title Missing moment.unix() Feature request: moment.unix() Aug 7, 2018
@leshakoss
Copy link
Contributor

The best way to do it right now is to use getTime function which returns the milliseconds timestamp.

Math.floor(getTime(new Date()) / 1000)

@envil
Copy link
Author

envil commented Aug 8, 2018

@leshakoss what I meant is to create a new Date object from the unix timestamp. I'm currently doing it like const date = new Date(epoch * 1000)

@envil envil changed the title Feature request: moment.unix() Feature request: create new date from unix timestamp like moment.unix(epochTime) Aug 8, 2018
@leshakoss
Copy link
Contributor

Oh, my bad. Every function in date-fns does accept a milliseconds timestamp instead of any date argument, so you can use them the same way that you're currently doing it: getISOWeeksInYear(epoch * 1000). Also parse from v1 and toDate from v2 could be used to convert ms timestamps to a date, and of course, using new Date constructor is a perfectly valid way to do it.

@envil
Copy link
Author

envil commented Aug 8, 2018

Yep, my situation is I have to initiate the date from epoch time quite a lot in my application, so having the * 1000 magic number everywhere is quite irritating to me. I could write my own date util to create the date like fromUnix(epoch) that can return the date, but then what's the point to use a some library like date-fns. Also not many people know how to create the date from the epoch time, so this would be quite helpful I think.

@Kingwl
Copy link
Contributor

Kingwl commented Sep 14, 2018

👍 helpful to me,
i prefer to explicit convert rather than * 1000

@kossnocorp kossnocorp added this to the Any release milestone Sep 24, 2018
@kossnocorp
Copy link
Member

That's a good idea and easy to implement. My API suggestion is two functions: fromUnix() and toUnix().

I'm strictly against being "smart" and trying to guess if it's JS time or Unix time because that would cause unpleasant bugs. Also given that you receive the time in seconds, it's vital to know that JS has time in milliseconds because likely you will send the time back to the back-end.

Also, I recommend using ISO-8601 strings for cross-language communications because it does not only save you from this kind of problems but also preserves the original time zone.

@Kingwl
Copy link
Contributor

Kingwl commented Sep 25, 2018

I have already sent a pr for toUnix but named getUnixTime
#870

@kossnocorp
Copy link
Member

@Kingwl oh wow, I did miss it 🤦‍♂️ Thank you a lot, I'll take a look.

@kossnocorp kossnocorp changed the title Feature request: create new date from unix timestamp like moment.unix(epochTime) Add fromUnixTime function Oct 1, 2018
@kossnocorp kossnocorp changed the title Add fromUnixTime function Add fromUnixTime function Oct 1, 2018
@xkizer
Copy link
Contributor

xkizer commented Oct 18, 2018

PR for this in #937

@kossnocorp
Copy link
Member

fromUnixTime was released as date-fns@2.0.0-alpha.24: https://gist.github.com/kossnocorp/a307a464760b405bb78ef5020a4ab136#v200-alpha24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants