getUnixTime.mjs 775 B

123456789101112131415161718192021222324252627
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name getUnixTime
  4. * @category Timestamp Helpers
  5. * @summary Get the seconds timestamp of the given date.
  6. *
  7. * @description
  8. * Get the seconds timestamp of the given date.
  9. *
  10. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  11. *
  12. * @param date - The given date
  13. *
  14. * @returns The timestamp
  15. *
  16. * @example
  17. * // Get the timestamp of 29 February 2012 11:45:05 CET:
  18. * const result = getUnixTime(new Date(2012, 1, 29, 11, 45, 5))
  19. * //=> 1330512305
  20. */
  21. export function getUnixTime(date) {
  22. return Math.trunc(+toDate(date) / 1000);
  23. }
  24. // Fallback for modularized imports:
  25. export default getUnixTime;