To Unix Time - CSharp System

CSharp examples for System:DateTime Unix

Description

To Unix Time

Demo Code


using System.Globalization;
using System.Diagnostics.Contracts;
using System.Diagnostics.CodeAnalysis;
using System;/*from   ww  w .  j av a2  s  .c o m*/

public class Main{
        public static string ToUnixTime(DateTime dateTime)
        {
            if (dateTime < BaseUtcDateTime)
            {
                throw new ArgumentException("Date time cannot be less than 01/01/1970.");
            }
            Contract.EndContractBlock();

            DateTime epoch = BaseUtcDateTime;
            TimeSpan range = dateTime - epoch;
            return Math.Floor(range.TotalSeconds).ToString(CultureInfo.InvariantCulture);
        }
}

Related Tutorials