0

I have a method that returns the timestamp which is a bigint.

The built in Date method only accepts a number to create a date time. Is there another function or method that can take a bigint and convert that to a date.

Essentially something similar to Java's DateTimeFormatter class.

Also if possible is there a way to format the date as a string with the pattern "yyyyMMdd" looking for something similar to Java's

DateTimeFormat.forPattern

the timestamp is milliseconds since epoch time

let time = 1630017759934 
Teddy Dong
  • 362
  • 1
  • 6
  • 20
  • 2
    The answer is yes to all your questions. Can you share your code that you have tried and errors you have come across? When you say you have a `bigint` which is a timestamp, can you elaborate with some samples too? – Tigger Aug 26 '21 at 22:39
  • What kind of date are you saving if you need bigint size? Unix epoch time size is 10 digits (13 if you messure milliseconds). If you have some extreme requirement for small time-messurement in unix epoch time and only need yyyyMMdd then you can always just divide your time to make it smaller and convert it to a number first. E.e. milliseconds persision to second persision. `const t = Math.floor(1630012381123 / 1000);` `const d = new date(t);` – Wirde Aug 26 '21 at 22:45
  • I believe I can cast the timestamp from bigint to a number given the unix epoch time (milliseconds). Just now wondering what is the best way to format the date to the required pattern – Teddy Dong Aug 26 '21 at 22:47
  • See [this question](https://stackoverflow.com/questions/4631928/convert-utc-epoch-to-local-date) – devlin carnate Aug 26 '21 at 22:48
  • The number type can represent all integers of up to 53 bits exactly. Your timestamp has at most 42 bits, so there is plenty of room. – President James K. Polk Aug 26 '21 at 23:02
  • Does this answer your question? [Convert a Unix timestamp to time in JavaScript](https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript) – Tigger Aug 26 '21 at 23:10
  • For formatting looking for something similar to Java's DateTimeFormat.forPattern – Teddy Dong Aug 26 '21 at 23:28

1 Answers1

1

With that timestamp you could just use regular number, it's size is 15 and with millisecond persision you only use size 13.

Here's some example code that might work for you:

const time = 1630017759934;
const timeInSeconds = Math.floor(time/1000);
const d = new date(timeInSeconds);
const dateFormatted = `${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, '0')}${date.getDate().toString().padStart(2, '0')}`;
Wirde
  • 400
  • 3
  • 14
  • I tested this instead I believe it works as well. ``` const time = 1630017759934; const d = new Date(Number(time)); Is getMonth and getDay zero based which is why the +1 is needed? – Teddy Dong Aug 27 '21 at 00:44
  • @TeddyDong— 1630017759934 is not an [ECMAScript BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt), it's just a number so `new Date(time)` is sufficient (returns a Date for 2021-08-26T22:42:39.934Z). Also, code in comments is wrapped in single escapes: \``code`\`. :-) Yes, months are zero indexed as are days, 0 is Sunday, 1 is Monday, etc. – RobG Aug 27 '21 at 03:01
  • @TeddyDong: It's like RobG says. Did this answer your question? If so, please accept it as the answer using the checkmark on the left. If not, is there something you still need help with? – Wirde Aug 27 '21 at 08:19
  • @RobG So getDate isn't what I need since it is day of week, is there a way to get the actual day (26) from 2021-08-26T22:42:39.934Z – Teddy Dong Aug 30 '21 at 19:21
  • @TeddyDong: Updated my post with the correct function. The one you are looking for is getDate. The example code should work now. – Wirde Aug 30 '21 at 20:39
  • 1
    @Wirde—you also need to pad single digit months and days with a leading zero. – RobG Aug 30 '21 at 21:42
  • so last thing I am using toDate() from `new Date(1630017759934)` however the day becomes 27 instead of 26. Is there a way to fix the timestamp or timezone? getUTCDate() also returns 27 – Teddy Dong Aug 30 '21 at 21:58
  • So with timestamp and Date you won't need the +1 for the `getDate()` method only `getMonth()`. Thanks – Teddy Dong Aug 30 '21 at 22:43
  • @RobG: Ah ofcourse, i updated the example with that in mind. TeddyDong: Try the new example code, should give you a date like 20210803 instead of 202183. – Wirde Aug 31 '21 at 08:48