SoFunction
Updated on 2025-04-11

3 ways to convert the current time into a timestamp in C#

Preface

Today we will talk about timestamps.

In the computer application world, whether to record the time of events, ensure data consistency or improve security, timestamps are indispensable and important tools, especially in preventing replay attacks and managing user sessions.

For example, in a bank application, login requests and transfer operations will carry timestamps.

The server side ensures that the request is fresh and not from previous communications by verifying these timestamps.

If the user does not perform any operations within a certain period of time, the system will automatically log out of the user's session.

In C#, converting the current time to a timestamp is a simple and easy thing to do,

Here are 3 ways to achieve this transformation, let’s take a look!

1. Use DateTime to convert directly

Can be usedTotalSecondsandTotalMillisecondsThe method is converted directly, but you need to calculate the difference between the current time and the Unix epoch by yourself.

The code is as follows,Pay attention to comments in the code

using System;

classProgram
{
    static void Main()
    {
        // Use to get the current UTC time        DateTime currentTime = ;

        // Calculate the time stamp by the difference from 1970-01-01 00:00:00        // The timestamp is usually calculated from the Unix epoch (i.e. 1970-01-01 00:00:00)        // If necessary, you can also customize the start time, such as 2001-01-01        // Calculate the timestamp (seconds)        long timestampInSeconds = (long)(currentTime - new DateTime(1970, 1, 1)).TotalSeconds;

        // Calculate timestamps (milliseconds)        long timestampInMilliseconds = (long)(currentTime - new DateTime(1970, 1, 1)).TotalMilliseconds;

        ("Timestamp of current time (seconds): " + timestampInSeconds);
        ("Timestamp of current time (milliseconds): " + timestampInMilliseconds);
    }
}

2. Use

If you need higher accuracy, you can useTo get the Ticks (100 nanosecond units) of the current UTC time.

However, this value is not a standard Unix timestamp, and it needs to be converted into a second or millisecond-level timestamp through simple mathematical operations.

using System;

classProgram
{
    static void Main()
    {
        // Get the current UTC time Ticks        long ticks = ;
        
        // Convert to milliseconds since the Unix Era        long unixEpochTicks = new DateTime(1970, 1, 1, 0, 0, 0, ).Ticks;
        long millisecondsSinceEpoch = (ticks - unixEpochTicks) / ;
        
        ($"Timestamp of current time(millisecond): {millisecondsSinceEpoch}");
    }
}

3. Use DateTimeOffset

Can also be usedDateTimeOffsetClasses to get timestamps, which will be more convenient when dealing with time zones.

using System;

classProgram
{
    static void Main()
    {
        // Get the current time        DateTimeOffset currentTime = ;

        // Calculate the timestamp (seconds)        long timestampInSeconds = ();

        // Calculate timestamps (milliseconds)        long timestampInMilliseconds = ();

        ("Timestamp of current time (seconds): " + timestampInSeconds);
        ("Timestamp of current time (milliseconds): " + timestampInMilliseconds);
    }
}

Summarize

In C#, converting the current time to a timestamp is a simple and important operation.

With the above 3 methods, you can easily get Unix timestamps, whether in seconds or milliseconds.

This is the article about the 3 methods of converting the current time to timestamp in C#. For more related content on C# time to timestamp, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!