Question about Unixtime to Timestamp
#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();
        }
 
}



Messages In This Thread
[No subject] - by Jaiz - 08-05-2013, 11:08 PM
[No subject] - by OldHM - 08-06-2013, 02:30 AM
[No subject] - by Paramount - 08-06-2013, 07:07 AM
[No subject] - by Jaiz - 08-06-2013, 09:38 AM
[No subject] - by HateMe - 08-06-2013, 03:37 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)