EPOCH TIME NOW

THE UNIX EPOCH TIME AND CONVERTERS

How to Get Epoch/UNIX Time in TypeScript

Unix/Epoch Time now:

In TypeScript, getting Epoch timestamps is straightforward, and there are multiple methods to accomplish this task. This article explores different ways to get Epoch timestamps in TypeScript and provides practical use cases.

Using the Date Object

In TypeScript, you can use the built-in Date object to fetch the current time and calculate the current Epoch timestamp using the getTime() method.





    const currentTime = new Date();

    const epochTime = Math.floor(currentTime.getTime() / 1000);

    document.write("Current Epoch timestamp: " + epochTime);



This code snippet utilizes the Date object to obtain the current time in milliseconds and then converts it to seconds to obtain the Epoch timestamp.

Use Case 1: Logging Timestamps





    const currentTime = new Date();

    const epochTime = Math.floor(currentTime.getTime() / 1000);

    const data = "Sensor data reading...";

    document.write(data + " - Timestamp: " + epochTime);



In this example, TypeScript code is adapted for logging data with timestamps. It obtains the current Epoch timestamp and combines it with sensor data for logging.

Use Case 2: Time Duration Calculation





    const startTime = new Date();



    // Perform a task or operation



    const endTime = new Date();

    const timeElapsed = Math.floor((endTime.getTime() - startTime.getTime()) / 1000);

    document.write("Time taken: " + timeElapsed + " seconds");



This use case demonstrates how to measure the time taken for a task. It records the start and end times using the Date object and calculates the time elapsed in seconds.

Using the performance.now() Method

You can also fetch the current Epoch timestamp using the performance.now() method, which provides a high-resolution time in milliseconds since the page started to load.





    const currentTime = performance.now();

    const epochTime = Math.floor(currentTime / 1000);

    document.write("Current Epoch timestamp: " + epochTime);



This code snippet directly uses performance.now() to obtain the current Epoch timestamp.

Use Case 3: Expiry Timestamp for Cache





    const currentTime = performance.now();

    const epochTime = Math.floor(currentTime / 1000);



    const cacheKey = "cached_data";

    const expiryTime = epochTime + 600; // 10 minutes from now

    document.write("Cached data that expires at " + expiryTime);



    if (epochTime > expiryTime) {

        document.write("Cache expired. Recaching data...");

        // Re-cache the data

    }



In this example, the TypeScript code calculates the expiry time for cache data using performance.now() and checks if the cache has expired.

Use Case 4: Event Scheduling





    const currentTime = performance.now();

    const epochTime = Math.floor(currentTime / 1000);

    const eventTime = epochTime + 3600; // 1 hour from now



    const checkEvent = setInterval(function() {

        const currentTime = performance.now();

        const currentEpochTime = Math.floor(currentTime / 1000);



        if (currentEpochTime >= eventTime) {

            clearInterval(checkEvent);

            document.write("Event occurred!");

        }

    }, 60000); // Check every minute



This use case demonstrates how to schedule an event to occur in the future. TypeScript code adds 3600 seconds (1 hour) to the current time to set the event time and continuously checks for its occurrence.

These code examples illustrate how to get Epoch/UNIX timestamps in TypeScript and showcase their applications in practical scenarios. Whether you need to log data with timestamps, calculate time durations, set cache expiry times, or schedule events, Epoch timestamps are versatile tools in TypeScript for precise time-related operations.