Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

c# UTC时间转时间戳

  1. User

    c# UTC时间转时间戳

  2. Assistant

    在C#中,可以使用以下代码将UTC时间转换为时间戳:

    DateTime dt = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc);
    int timestamp = (int)(DateTime.UtcNow - dt).TotalSeconds;
    

    其中,DateTime.UtcNow获取当前UTC时间;(DateTime.UtcNow - dt).TotalSeconds计算当前时间与1970年1月1日0时0分0秒的时间间隔(单位为秒),并将其转换为整数类型赋值给timestamp变量。

    需要注意的是,大多数时间戳是以秒为单位的整数,而有些时间戳可能是以毫秒或微秒为单位的,需要根据具体情况进行转换。