Converts a datetime object to unix format depending in the chosen type (Milliseconds|Seconds) - CSharp System

CSharp examples for System:DateTime Unix

Description

Converts a datetime object to unix format depending in the chosen type (Milliseconds|Seconds)

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from  ww w.  j  ava 2  s .c  om

public class Main{
        /// <summary>
        /// Converts a datetime object to unix format depending in
        /// the chosen type (Milliseconds|Seconds)
        /// </summary>
        /// <param name="dateTime">DateTime object to be converted</param>
        /// <param name="type">Type of the value to be retrieved</param>
        /// <returns></returns>
        public static long DateTimeToUnixTime(DateTime dateTime, UnixTimeType type) {
            TimeSpan timeSpan = dateTime - EPOCH;

            if (type == UnixTimeType.Milliseconds) {
                return (long)timeSpan.TotalMilliseconds;
            } else {
                return (long)timeSpan.TotalSeconds;
            }
        }
}

Related Tutorials