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

get unix timestamp of given date #372

Closed
ravins opened this issue Jan 6, 2017 · 10 comments · Fixed by #870
Closed

get unix timestamp of given date #372

ravins opened this issue Jan 6, 2017 · 10 comments · Fixed by #870

Comments

@ravins
Copy link

ravins commented Jan 6, 2017

Is it current supported? In moment we have moment.valueOf()

@leshakoss
Copy link
Contributor

@ravins right now you can use standard JavaScript method date.getTime() (or date.valueOf() which does the same for dates). If you have a string-formatted date, you can use dateFns.parse to convert it to Date beforehand:

var parse = require('date-fns/parse')
var timestamp = parse('2017-01-06').getTime() //=> 1483660800000
// Please note that your timestamp can differ from mine
// because dates are created in the local timezone

But I think this indeed is a gap of the library so we'll probably make a shortcut function for (converting to dates + getting timestamp) ASAP. Stay tuned!

@leshakoss leshakoss self-assigned this Jan 6, 2017
@kossnocorp
Copy link
Member

One of the key values of date-fns is to not repeat existing API, but encourage to use the built-in functionality as long as it's pitfall-free. Hence this is a documentation, not a feature gap.

@leshakoss
Copy link
Contributor

@kossnocorp don't forget that we already have functions that repeat functionality of JavaScript standard library (like getSeconds) but additionally convert arguments to Date (so you can use an ISO 8601 formatted string or a timestamp as a date)

@kossnocorp
Copy link
Member

Nevermind then 🤣

@leshakoss
Copy link
Contributor

@ravins hello! Released the function with v1.26.0: https://date-fns.org/docs/getTime

@uLan08
Copy link

uLan08 commented Jul 4, 2018

the issue says unix timestamp but shouldn't unix timestamps be in seconds not milliseconds?

@kossnocorp
Copy link
Member

@uLan08 since date-fns is a JS library where every time value is in milliseconds (i.e. setTimeout/setInterval) for consistency sake we keep everything in ms.

The next v2 release will include getUnixTime: #870

@kossnocorp
Copy link
Member

getUnixTime was released as date-fns@2.0.0-alpha.20. See the change log: https://gist.github.com/kossnocorp/a307a464760b405bb78ef5020a4ab136#v200-alpha20

@marcus-hiles
Copy link

@ravins hello! Released the function with v1.26.0: https://date-fns.org/docs/getTime

Hi @ravins the solution here isn't complete. The result is in milliseconds you need to divide by 1000 to get unix timestamp value. Like below :

var result = getTime(new Date(2012, 1, 29, 11, 45, 5, 123)) / 1000

@marcus-hiles
Copy link

marcus-hiles commented Mar 12, 2019

getUnixTime was released as date-fns@2.0.0-alpha.20. See the change log: https://gist.github.com/kossnocorp/a307a464760b405bb78ef5020a4ab136#v200-alpha20

@kossnocorp where can we find the documentation on the new function getUnixTime pelase, can't seem to locate it on the provided link?

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

Successfully merging a pull request may close this issue.

5 participants