DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Coding Once, Thriving Everywhere: A Deep Dive Into .NET MAUI’s Cross-Platform Magic
  • How To Build Cross-Platform Applications Using Rust, Tauri and Svelte
  • Boosting Efficiency With Cross-Platform Code-Sharing Techniques
  • Unleashing Cross-Platform App Development: Mastering the Vital Tech Stack for Unprecedented Success

Trending

  • Understanding Git
  • Using Open Source for Data Integration and Automated Synchronizations
  • Three Ways AI Is Reshaping DevSecOps
  • Supercharge Your Software Development Journey With DevOps, CI/CD, and Containers
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Converting to/from Unix Timestamp in C#

Converting to/from Unix Timestamp in C#

Daniel D'agostino user avatar by
Daniel D'agostino
·
May. 26, 15 · Interview
Like (1)
Save
Tweet
Share
93.52K Views

Join the DZone community and get the full member experience.

Join For Free

a few days ago, visual studio 2015 rc was released. among the many updates to .net framework 4.6 with this release, we now have some new utility methods allowing conversion to/from unix timestamps.

although these were added primarily to enable more cross-platform support in .net core framework , unix timestamps are also sometimes useful in a windows environment. for instance, unix timestamps are often used to facilitate redis sorted sets where the score is a datetime (since the score can only be a double ).

unix timestamp conversion before .net 4.6

until now, you had to implement conversions to/from unix time yourself. that actually isn’t hard to do. by definition , unix time is the number of seconds since 1st january 1970, 00:00:00 utc. thus we can convert from a local datetime to unix time as follows:

            var datetime = new datetime(2015, 05, 24, 10, 2, 0, datetimekind.local);
            var epoch = new datetime(1970, 1, 1, 0, 0, 0, datetimekind.utc);
            var unixdatetime = (datetime.touniversaltime() - epoch).totalseconds;

we can convert back to a local datetime as follows:

var timespan = timespan.fromseconds(unixdatetime);
            var localdatetime = new datetime(timespan.ticks).tolocaltime();

unix timestamp conversion in .net 4.6

quoting the visual studio 2015 rc release notes :

new methods have been added to support converting datetime to or from unix time. the following apis have been added to datetimeoffset:

  • static datetimeoffset fromunixtimeseconds(long seconds)
  • static datetimeoffset fromunixtimemilliseconds(long milliseconds)
  • long tounixtimeseconds()
  • long tounixtimemilliseconds()

so .net 4.6 gives us some new methods, but to use them, you’ll first have to convert from datetime to datetimeoffset. first, make sure you’re targeting the right version of the .net framework:

csunixtime-frameworkversion

you can then use the new methods:

var datetime = new datetime(2015, 05, 24, 10, 2, 0, datetimekind.local);
            var datetimeoffset = new datetimeoffset(datetime);
            var unixdatetime = datetimeoffset.tounixtimeseconds();

…and to change back…

var localdatetimeoffset = datetimeoffset.fromunixtimeseconds(unixdatetime)
        .datetime.tolocaltime();
Cross platform .NET Framework Convert (command) Release (agency) Redis (company)

Published at DZone with permission of Daniel D'agostino, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Coding Once, Thriving Everywhere: A Deep Dive Into .NET MAUI’s Cross-Platform Magic
  • How To Build Cross-Platform Applications Using Rust, Tauri and Svelte
  • Boosting Efficiency With Cross-Platform Code-Sharing Techniques
  • Unleashing Cross-Platform App Development: Mastering the Vital Tech Stack for Unprecedented Success

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: