본문 바로가기

C# Winform

C# Winform | DateTime Convert(Long To, TimeStamp to)

by C기억저장소 2020. 4. 28.

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(1970110000);
    return origin.AddSeconds(timestamp);
}
cs
 

 

 

  DateTime To Timestamp

 

public long DateTimeToTimestamp(DateTime date)
{
    DateTime origin = new DateTime(1970110000);
    TimeSpan diff = date - origin;
    return Convert.ToInt64(diff.TotalSeconds);
}
cs
 
 

 

  TEST CODE

 

private void button1_Click(object sender, EventArgs e)
{
    DateTime test = new DateTime(2020428746300);
    long timestamp = DateTimeToTimestamp(test);
 
    Console.WriteLine(timestamp);
    Console.WriteLine(TimestampToDateTime(timestamp));
}
cs
 
 

 

  output

1588059990

2020-04-28 오전 7:46:30