Question about Unixtime to Timestamp
#1

Im very interessted how to make a datetime to unixtime timestamp. It is difficult or? Can somebody tell me more about it? Because i want to create some the unix timestamp for dcs or mcs etc. But i havent a idea how Smile

Maybe i want get this to work: 2 

but how i telled now idea. I hope somebody can help me with it 

 

Regards Jaiz Smile

#2

Just get the "utc" tool its an unix timecode tool

Smile

If you can find ill upload

#3

What about:

 

private static readonly DateTime UnixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static long GetCurrentUnixTimestampMillis()
{
return (long) (DateTime.UtcNow - UnixEpoch).TotalMilliseconds;
}

public static DateTime DateTimeFromUnixTimestampMillis(long millis)
{
return UnixEpoch.AddMilliseconds(millis);
}

public static long GetCurrentUnixTimestampSeconds()
{
return (long) (DateTime.UtcNow - UnixEpoch).TotalSeconds;
}

public static DateTime DateTimeFromUnixTimestampSeconds(long seconds)
{
return UnixEpoch.AddSeconds(seconds);
}

#4


Just get the "utc" tool its an unix timecode tool

Smile

If you can find ill upload

I dont want use this utc tool. I want my own one for me 

#5

i did poste already the functions for this but here its again

 

 

 

public static class DateTimeExtender
    {
        #region Functions
        /// <summary>
        /// Methods to convert DateTime to Unix time stamp
        /// </summary>
        /// <param name="_UnixTimeStamp">Unix time stamp to convert</param>
        /// <returns>Return Unix time stamp as long type</returns>
        public static double ToUnixTimestamp(this DateTime thisDateTime)
        {
            TimeSpan _UnixTimeSpan = (thisDateTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            return (double)_UnixTimeSpan.TotalSeconds;
        }
        
        #endregion
    }
 
public partial class Form1 : Form
    {
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {   // Unix timestamp is seconds past epoch
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
            DateTime localTime = TimeZoneInfo.ConvertTime(dtDateTime, TimeZoneInfo.Utc);
            return localTime;
        }
 
private void button_unix_tonormal_Click(object sender, EventArgs e)
        {
            string format = "dd.MM.yyyy HH:mmConfuseds";
            dateTimePicker_date_time.Text = UnixTimeStampToDateTime(double.Parse(textBox_unixtime.Text)).ToString(format);
        }
 
private void button_normal_to_unix_Click(object sender, EventArgs e)
        {
            // dd.MM.yyyy hh:mmConfuseds

            string picker = dateTimePicker_date_time.Text;
            string[] split_date_time = picker.Split( );
            string[] split_date = split_date_time[0].Split(.);
            string[] split_time = split_date_time[1].Split(Smile;

            int year = int.Parse(split_date[2]);
            int month = int.Parse(split_date[1]);
            int day = int.Parse(split_date[0]);

            int hours = int.Parse(split_time[0]);
            int minutes = int.Parse(split_time[1]);
            int seconds = int.Parse(split_time[2]);
            
            DateTime dt = new DateTime(year, month, day, hours, minutes, seconds, DateTimeKind.Utc);
            textBox_unixtime.Text = dt.ToUnixTimestamp().ToString();
 
            // or
            // textBox_unixtime.Text = DateTime.UtcNow.ToUnixTimestamp().ToString();
        }
 
}



Forum Jump:


Users browsing this thread: 1 Guest(s)