gpt4 book ai didi

c# - 如何在 int 中存储转换日期时间?

转载 作者:行者123 更新时间:2023-12-05 01:47:52 25 4
gpt4 key购买 nike

我需要将 DateTime 存储在 int 中。所以我尝试了以下代码

Int64 n = Int64.Parse(DateTime.Today.ToString("dd-MM-yyyy"));

Int64 twoday_date=Convert.ToInt64(System.DateTime.Today.ToString("dd-MM-yyyy"));

但是显示错误:

Input string was not in a correct format.

哪里出错了?

最佳答案

只需使用 DateTime.Ticks相反 - 绝对没有理由在这里开始与字符串相互转换。

long ticks = DateTime.Today.Ticks;

// Later in the code when you need a DateTime again
DateTime dateTime = new DateTime(ticks);

请注意,这将使用本地 日期 - 如果您尝试保留全局时间戳,则应使用 DateTime.UtcNow 而不是 DateTime。今天

如果你真的需要 int 而不是 long,你可能应该翻译和缩放,例如自 Unix 纪元以来的秒数。

关于c# - 如何在 int 中存储转换日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21062621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com