• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / C#: Unix Time to .Net DateTime and Vice Versa

C#: Unix Time to .Net DateTime and Vice Versa

April 18, 2009 by Sze Hau 1 Comment

For some reasons, some programmer might store time values as Unix timestamp in database instead of standard SQL DateTime data type. The main reason to store time values as Unix timestamp is unknown but what I guess is for maximum database compatibility. Some old database does not support SQL DateTime data type.

Converting Unix time to C# or .Net DateTime and vice versa is pretty simple. The unix time is simple the difference between the wanted DateTime value with the DateTime value of 1st January 1970. Here is the code in C#:

using System;

namespace snippetit.sample
{
  struct UnixTime
  {
    private static DateTime BEGIN_UTC = new DateTime(1970, 1, 1);
    private long utValue;

    public UnixTime(long seconds)
    {
      utValue = seconds;
    }

    public UnixTime(DateTime dateTime)
    {
      this.DateTime = dateTime;
    }

    public long Value
    {
      get { return utValue; }
      set { utValue = value; }
    }

    public DateTime DateTime
    {
      get { return BEGIN_UTC.AddSeconds((long)utValue); }
      set { utValue = (long)((TimeSpan)(value - BEGIN_UTC)).TotalSeconds; }
    }

    public override string ToString()
    {
      return DateTime.ToString("yyy-MM-dd HH:mm:ss"); ;
    }
  }
}

More from my site

  • Wordpress: How To Change Admin UsernameWordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And FilesLinux: How To Compress And Decompress Folders And Files
  • Java: How To Select Top N Objects From A ListJava: How To Select Top N Objects From A List
  • Java: How To Implement ungetc in JavaJava: How To Implement ungetc in Java
  • Java: How To Create A Simple Web Server Using HttpServerJava: How To Create A Simple Web Server Using HttpServer
  • Java: Format Integer Into Fixed Width StringJava: Format Integer Into Fixed Width String

Filed Under: Programming and Scripting Tagged With: C#, DateTime, how to, Unix time

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Trackbacks

  1. Blogs Updates for February 2009 - szehau’s weblog says:
    May 3, 2009 at 2:23 pm

    […] C#: Unix Time to .Net DateTime and Vice Versa […]

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in