C# Winform | DateTime Convert(Long To, TimeStamp to)
long Date to DateTime
private DateTime LongDateToDateTime(long longdate, string type)
{
DateTime date = DateTime.ParseExact(longdate.ToString(), type, System.Globalization.CultureInfo.InvariantCulture);
return date;
}
|
cs |
TEST CODE
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(LongDateToDateTime(20200505093059, "yyyyMMddHHmmss"));
Console.WriteLine(LongDateToDateTime(20200505, "yyyyMMdd"));
}
|
cs |
output
2020-05-05 오전 9:30:59
2020-05-05 오전 12:00:00
Timesptamp To DateTime
public DateTime TimestampToDateTime(long timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}
|
cs |
DateTime To Timestamp
public long DateTimeToTimestamp(DateTime date)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = date - origin;
return Convert.ToInt64(diff.TotalSeconds);
}
|
cs |
TEST CODE
private void button1_Click(object sender, EventArgs e)
{
DateTime test = new DateTime(2020, 4, 28, 7, 46, 30, 0);
long timestamp = DateTimeToTimestamp(test);
Console.WriteLine(timestamp);
Console.WriteLine(TimestampToDateTime(timestamp));
}
|
cs |
output
1588059990
2020-04-28 오전 7:46:30
'C# Winform' 카테고리의 다른 글
C# Winform | enum foreach (0) | 2020.05.06 |
---|---|
C# Winform | Hardware Info | CPU ID, HDD, Mac, Serial (0) | 2020.04.29 |
C# Winform | properties settings 저장 (0) | 2020.03.05 |
C# Winform | cross thread - InvalidOperationException (0) | 2020.03.02 |
C# Winform | Notification Popup (0) | 2020.02.28 |