Getting the Unix timestamp value
Code examples for generating Unix timestamps in C#, JavaScript and Python, with notes on seconds, milliseconds and UTC handling.
A Unix timestamp is the number of seconds elapsed since 1 January 1970 (UTC). Reducing time to a single number makes it easy to store, compare and move between systems — no time zone or format arguments.
The C# and JavaScript equivalents are below. Their units differ: the C# example produces seconds, the JavaScript one milliseconds. Ignoring that when passing values between the two is the most common reason dates end up in 1970 or thousands of years in the future.
One more point: a timestamp is always UTC. Convert to local time when displaying, never when storing.
long timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
var timestamp=(new Date()) / 1;
import time
timestamp = int(time.time())
Short guide
When to use it
Unix timestamps are useful in session handling, API logs, queue jobs, payment callbacks and almost any place where dates move between systems. In web and mobile applications, they give the client and server a simple shared language for time.
What to watch
- Be clear about seconds versus milliseconds; JavaScript often uses milliseconds while many backend systems use seconds.
- Store the value in UTC and convert it only when displaying it to the user.
- Choose between timestamp and date columns based on filtering, reporting and readability needs.
Common mistake
The common mistake is treating a timestamp as local time. The value itself does not carry a time zone, so display logic still needs a conversion step.
Where this example helps
Small helper snippets like this save time in web applications, mobile app backends, admin panels and API projects. The important part is not copying the example blindly, but adapting it to your data format, security needs and performance expectations.