![]() |
|
Question about Unixtime to Timestamp - Printable Version +- LCKB (https://lckb.dev/forum) +-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109) +--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196) +---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192) +---- Thread: Question about Unixtime to Timestamp (/showthread.php?tid=2243) |
- Jaiz - 08-05-2013 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 . Maybe i want get this to work: 2 but how i telled now idea. I hope somebody can help me with it Regards Jaiz ![]() - OldHM - 08-06-2013 Just get the "utc" tool its an unix timecode tool ![]() If you can find ill upload - Paramount - 08-06-2013 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); } - Jaiz - 08-06-2013 Just get the "utc" tool its an unix timecode tool ![]() If you can find ill upload I dont want use this utc tool. I want my own one for me - HateMe - 08-06-2013 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:mm s";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:mm sstring 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( ;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(); } } |