Hap*_*ppy 2 .net c# datetime type-conversion winforms
我需要在Int中存储DateTime.所以我试过下面的代码
Int64 n = Int64.Parse(DateTime.Today.ToString("dd-MM-yyyy"));
Run Code Online (Sandbox Code Playgroud)
要么
Int64 twoday_date=Convert.ToInt64(System.DateTime.Today.ToString("dd-MM-yyyy"));
Run Code Online (Sandbox Code Playgroud)
但它显示错误:
输入字符串的格式不正确.
错误在哪里?
只需使用DateTime.Ticks- 这里绝对没有理由开始转换为字符串.
long ticks = DateTime.Today.Ticks;
// Later in the code when you need a DateTime again
DateTime dateTime = new DateTime(ticks);
Run Code Online (Sandbox Code Playgroud)
请注意,这将使用本地日期 - 如果您尝试保留全局时间戳,则应使用DateTime.UtcNow而不是DateTime.Today.
如果你真的需要int代替long,你可能应该翻译和扩展,例如自Unix时代以来的秒数.