skip to Main Content

I have Time value like this :

const timeValue = new Date('2023-06-13T12:34:56.789123Z');

I need to convert this data to Unix Timestamp with Microseconds precisions, but method Date.getTime() only support Millisecond. What should I do to solve this issue?

2

Answers


  1. You can use this package: https://www.npmjs.com/package/microseconds if you are using node.

    It uses process.hrtime() or performance.now() if available. You can also do this yourself by using these functions but the package already does the necessary calculations. Be aware, that both these possible functions may not be available sometimes. In these cases, the package will simply fall back to Date.now()*1000.

    Login or Signup to reply.
  2. // Obtain time value (e.g., Date object, date string, or numeric value)
    const timeValue = Date.parse('2023-06-13T12:34:56.789Z');
    
    // Convert time value to Unix timestamp with microsecond accuracy
    const unixTimestampMicro = timeValue / 1000 * 1000000;
    
    console.log(unixTimestampMicro);
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search