Convert System.DateTime to UNIX timestamp

CK CK 3776 
Created at
Updated at  
380 0 0

In case of development in clinet/server application, datetime formation conversion is frequently required.

TimeSpan is a .NET data structure that represents a duration of time, often used to calculate the difference between two dates or times. It can hold values like days, hours, minutes, seconds, and milliseconds, and allows you to perform various operations on these durations, such as addition, subtraction, and comparison.

Following is the simple example for datetime conversion.

private double ConvertToTimestamp(DateTime value)
{
    //create Timespan by subtracting the value provided from
    //the Unix Epoch
    TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

    //return the total seconds (which is a UNIX timestamp)
    return (double)span.TotalSeconds;
}



 

Tags Client/Server Application DateTime Conversion TimeSpan UNIX TimeStamp Facebook X
Comments 0
Similar posts
  1. Retrieve system information
    494
  1. How to implement switch statement in C# ?
    983
  2. How to implement while loop in C# ?
    1,334
  3. How to delete file ?
    1,186
  4. How to convert integer to text string ?
    695
  5. How to convert text string to integer ?
    608
  6. How to set an certain color as transparent color on bitmap
    508
  7. How to resize and save PNG or Jpeg Image file ?
    629
  8. How to display message box in C#
    1,101
  9. How to convert bitmap image to jpeg image ?
    352
  10. To load image from file
    445
  11. URL Encode, Decode function for MFC
    601
  12. How to load HTML resource on MFC ?
    838
  13. How to get GM Time as String ?
    422
  14. Makefile to automate the build process for C++
    275
  15. Finding process name on OS
    404
  16. Retrieve system information
    494
  17. How to call SetTimer function on MFC CDialog class ?
    650
  18. How to rename(change file name) in C# ?
    1,071
  19. Find Files in certain directory
    922
  20. Implementing C#'s foreach loop in Delphi 8
    459
  21. How to detect resource leaks in a Form
    936
  22. The best way to enable and disable buttons, menu items and toolbar buttons
    666
  23. How to exit from a console application
    809
  24. How to restrict a program to a single instance
    726
  25. Compress and decompress strings in C#
    969
  26. Graphics in C# - Irregular Forms
    904
  27. Extracting the Country from IP Address
    463
  28. How to run shell command by MFC ?
    434
  29. Binary Search Sample Code
    403
  30. Double Linked List based in C++
    796
Recently updated
  1. How do I create animated background for Google Meet?
    19,000
  2. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    192
  3. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    75
  4. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    151
  5. Array of arguments passed to script in PHP
    83
  6. Invalid Auction Storms in Ad Tech
    91
  7. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    194
  8. The Two Clocks of Ad Server Architecture
    61
  9. Frequency Cap Architecture Overview
    505
  10. Collecting Proof-of-Play from 100,000 Screens
    139
  11. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    276
  12. What is RedisVL?
    102
  13. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    218
  14. TPU & GPU Cluster Management Tools
    229
  15. The Concept of Scaffolding in AI Agents
    217
  16. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    568
  17. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    246
  18. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    321
  19. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    196
  20. Machine Learning: Training vs. Ranking
    200
  21. The Continuous Learning Machine: Designing a Loop That Actually Turns
    212
  22. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    188
  23. Designing a Data Curation System Around a Vector Database
    212
  24. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    228
  25. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    646
  26. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    179
  27. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    203
  28. Understanding Data Lineage
    251
  29. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    159
  30. GPU & LLM Cluster Infrastructure & Task Scheduling Architecture
    229