Convert a DateTime to a UNIX timestamp in milliseconds. - CSharp System

CSharp examples for System:DateTime Convert

Description

Convert a DateTime to a UNIX timestamp in milliseconds.

Demo Code

// All rights reserved.
using System.Runtime.Serialization;
using System.Reflection;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;/*from   w w w  .ja v a  2 s . co  m*/

public class Main{
        /// <summary>
        /// Convert a <c>DateTime</c> to a UNIX timestamp in milliseconds.
        /// </summary>
        static public double ConvertToTimestamp(DateTime value)
        {
            var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            if (value.Kind != DateTimeKind.Utc)
                origin = origin.ToLocalTime();

            return (value - origin).TotalSeconds * 1000;
        }
}

Related Tutorials