I'm trying to convert the current date to an Uint8 array of bytes in little endian format.
What I tried is this :
const epochSeconds = new Date().valueOf();
This works and returns the current date in epoch timestamp format.
I then create an array buffer of 8 bytes and a dataview and try to add the bytes to it, but it seems incorrect
const byteArray = this.longToByteArray(epochSeconds);
const buffer = new ArrayBuffer(8);
const dataView = new DataView(buffer);
for (let i = 0; i < 8; i++) {
dataView.setUint8(i, byteArray[i]);
}
I tried to verify that value with a python program
import datetime
uint8_array = [25, 15, 142, 103, 0, 0, 0, 0]
#uint8_array = [72, 224, 87, 131, 148, 1, 0, 0]
timestamp_little_endian = int.from_bytes(uint8_array, byteorder='little')
human_readable_date_little_endian = datetime.datetime.utcfromtimestamp(timestamp_little_endian)
print(timestamp_little_endian, human_readable_date_little_endian)
With the first uint8_array i get 1737363225 2025-01-20 08:53:45
and with the second
File "a.py", line 8, in <module>
human_readable_date_little_endian = datetime.datetime.utcfromtimestamp(timestamp_little_endian)
OSError: [Errno 22] Invalid argument
What am I doing wrong ?
I'm trying to convert the current date to an Uint8 array of bytes in little endian format.
What I tried is this :
const epochSeconds = new Date().valueOf();
This works and returns the current date in epoch timestamp format.
I then create an array buffer of 8 bytes and a dataview and try to add the bytes to it, but it seems incorrect
const byteArray = this.longToByteArray(epochSeconds);
const buffer = new ArrayBuffer(8);
const dataView = new DataView(buffer);
for (let i = 0; i < 8; i++) {
dataView.setUint8(i, byteArray[i]);
}
I tried to verify that value with a python program
import datetime
uint8_array = [25, 15, 142, 103, 0, 0, 0, 0]
#uint8_array = [72, 224, 87, 131, 148, 1, 0, 0]
timestamp_little_endian = int.from_bytes(uint8_array, byteorder='little')
human_readable_date_little_endian = datetime.datetime.utcfromtimestamp(timestamp_little_endian)
print(timestamp_little_endian, human_readable_date_little_endian)
With the first uint8_array i get 1737363225 2025-01-20 08:53:45
and with the second
File "a.py", line 8, in <module>
human_readable_date_little_endian = datetime.datetime.utcfromtimestamp(timestamp_little_endian)
OSError: [Errno 22] Invalid argument
What am I doing wrong ?
Share Improve this question asked Jan 20 at 14:11 aleksshrvaleksshrv 11 silver badge 1- @robertklep has your answer. Just divide the ECMAScript time value by 1e3 (or just remove the last 3 digits). – RobG Commented Jan 23 at 3:41
1 Answer
Reset to default 1The first value is a timestamp in seconds, the second one in milliseconds, which is the resolution that Javascript uses for timestamps.
More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf