Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in Go

How to convert Datetime to Unix timestamp in Go

🎈 🎈 🎈
+1

    package main

    import (
        "fmt"
        "time"
    )
    
    func main() {
        dateTime := time.Date(1999, 12, 16, 22, 18, 59, time.UTC)
        unixTimestamp := dateTime.Unix()
    
        fmt.Println("Unix Timestamp:", unixTimestamp)
    }            
        

Output:
Unix Timestamp: 945382739
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Go example, the time.Date() function is used to create a time.Time object representing the specific date and time, eg 2023-10-28 12:44:12.
The Unix() method is then called on this time.Time object to convert it to a Unix timestamp, which represents the number of seconds since the Unix epoch (January 1, 1970). The resulting Unix timestamp, eg 1698499452, is then printed.



Other useful Go date functions

Function Description Example
time.Now() Returns the current date and time as a time.Time object. time.Now() returns 2024-08-28 12:34:56 +0000 UTC
time.Date() Creates a new time.Time object for a specific date and time. time.Date(2024, 8, 28, 12, 34, 56, 0, time.UTC) returns 2024-08-28 12:34:56 +0000 UTC
time.Unix() Converts a Unix timestamp (seconds since epoch) to a time.Time object. time.Unix(1698499452, 0) returns 2023-10-28 12:44:12 +0000 UTC
time.Parse() Parses a formatted string and returns the corresponding time.Time object. time.Parse("2006-01-02 15:04:05", "2024-08-28 12:34:56") returns 2024-08-28 12:34:56 +0000 UTC
time.Format() Formats a time.Time object into a string according to the specified layout. time.Now().Format("2006-01-02 15:04:05") returns 2024-08-28 12:34:56
time.Add() Adds a duration to a time.Time object, returning a new time.Time. time.Now().Add(24 * time.Hour) returns the same time tomorrow.
time.Sub() Subtracts one time.Time object from another, returning the duration between them. time.Now().Sub(pastTime) returns the duration since pastTime.
time.Since() Returns the duration since the specified time.Time. time.Since(startTime) returns the duration since startTime.
time.Sleep() Pauses the current goroutine for at least the specified duration. time.Sleep(2 * time.Second) pauses for 2 seconds.
time.UTC() Returns a time.Time object corresponding to the specified UTC time. time.UTC(2024, 8, 28, 12, 34, 56, 0) returns 2024-08-28 12:34:56 +0000 UTC
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder