
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to get the Unix timestamp in C#
A Unix timestamp is mainly used in Unix operating systems. But it is helpful for all operating systems because it represents the time of all time zones.
Unix Timestamps represent the time in seconds. The Unix epoch started on 1st January 1970.
So, Unix Timestamp is the number of seconds between a specific date
Example
to get the Unix Timestamp Using DateTime.Now.Subtract().TotalSeconds Method
class Program{ static void Main(string[] args){ Int32 unixTimestamp = (Int32)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; Console.WriteLine("The Unix Timestamp is {0}", unixTimestamp); Console.ReadLine(); } }
Output
1596837896
Example
to get the Unix Timestamp Using DateTimeOffset.Now.ToUnixTimeSeconds() Method
class Program{ static void Main(string[] args){ var unixTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); Console.WriteLine("The Unix Timestamp is {0}.", unixTimestamp); Console.ReadLine(); } }
Output
1596819230.
Example
to Get the Unix Timestamp Using TimeSpan Struct Methods
class Program{ static void Main(string[] args){ TimeSpan epochTicks = new TimeSpan(new DateTime(1970, 1, 1).Ticks); TimeSpan unixTicks = new TimeSpan(DateTime.Now.Ticks) - epochTicks; Int32 unixTimestamp = (Int32)unixTicks.TotalSeconds; Console.WriteLine("The Unix Timestamp is {0}.", unixTimestamp); Console.ReadLine(); } }
Output
1596839083
- Related Articles
- Convert MySQL timestamp to UNIX Timestamp?
- How to convert from Unix timestamp to MySQL timestamp value?
- How to convert MySQL datetime to Unix timestamp?
- How to convert Python date to Unix timestamp?
- How to convert a Unix timestamp to time in JavaScript?
- Get unix timestamp from string without setting default timezone in PHP
- How to convert unix timestamp string to readable date in Python?
- Convert MM/DD/YY to Unix timestamp in MySQL?
- Convert MySQL Unix-Timestamp format to date format?
- MySQL - Convert YYYY-MM-DD to UNIX timestamp
- Convert dd/mm/yyyy string to Unix timestamp in MySQL?
- Convert UNIX timestamp into human readable format in MySQL?
- How to get a timestamp in JavaScript?
- How to get timestamp using MySQL?
- How to get time in milliseconds since the Unix epoch in JavaScript?

Advertisements