Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-04-26
Convert Datetime to Unix Timestamp in Dart

How to convert Datetime to Unix timestamp in Dart

🎈 🎈 🎈
+1

    void main() {
        DateTime dateTime = DateTime.utc(2004, 8, 18, 14, 26, 41);  // Example date
        int unixTimestamp = dateTime.millisecondsSinceEpoch ~/ 1000;
        
        print('Unix Timestamp: $unixTimestamp');
        }              
        

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

Code Explanation

In this Dart example, a DateTime object is created for the date '2022-05-15 08:10:45' UTC. The millisecondsSinceEpoch property is used to get the time in milliseconds since the Unix epoch (January 1, 1970), and the result is divided by 1000 to convert it to seconds (the Unix timestamp format).
The Unix timestamp '1652602245' is then printed.



Other useful Dart date functions

Function Description Example
DateTime.now() Returns the current date and time based on the local time zone. DateTime.now() returns 2024-08-28 12:34:56
DateTime.utc() Creates a DateTime object in UTC for the specified year, month, day, hour, minute, and second. DateTime.utc(2022, 5, 15, 8, 30, 45) returns 2022-05-15 08:10:45 UTC
DateTime.fromMillisecondsSinceEpoch() Creates a DateTime object from the number of milliseconds since January 1, 1970 (the Unix epoch). DateTime.fromMillisecondsSinceEpoch(1652602245000) returns 2022-05-15 08:10:45 UTC
DateTime.parse() Parses a string into a DateTime object. DateTime.parse("2022-05-15T08:10:45Z") returns 2022-05-15 08:10:45 UTC
millisecondsSinceEpoch Returns the number of milliseconds since January 1, 1970 for a DateTime object. dateTime.millisecondsSinceEpoch returns 1652602245000
toIso8601String() Converts a DateTime object to a string in ISO 8601 format. dateTime.toIso8601String() returns 2022-05-15T08:10:45Z
add() Adds a Duration to a DateTime object. dateTime.add(Duration(days: 5)) returns 2022-05-20 08:10:45
subtract() Subtracts a Duration from a DateTime object. dateTime.subtract(Duration(days: 5)) returns 2022-05-10 08:10:45
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-04-26
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder