C# 从刻度到日期的格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1129410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:53:08  来源:igfitidea点击:

Format from ticks to date

c#

提问by Amnis

I am needing to transfer some logs which were timestamped in ticks to an XML document. I would prefer the timestamps to be more Specific such as "July 14, 2009 10:18:04 pm"

我需要将一些带有时间戳的日志传输到 XML 文档。我希望时间戳更具体,例如“2009 年 7 月 14 日 10:18:04 pm”

I was planning to using something along the line of:

我计划使用以下内容:

DateTime logDate = DateTime.Parse(logText);
logDate.ToString("MMM dd yyyy hh:mm:ss tt");

I figured this would be OK as DateTime.Now.Ticks is how you can get ticks. It is however returning that it is not a proper DateTime format. during setting logDate.

我认为这没问题,因为 DateTime.Now.Ticks 是您获取滴答声的方法。然而,它返回它不是一个正确的 DateTime 格式。在设置 logDate 期间。

I am sure there is a simple solution but I just can't come across it.

我确信有一个简单的解决方案,但我无法找到它。

采纳答案by CMS

If logTextis a string, you can convert it to long (Int64) and use this constructor:

如果logText是字符串,则可以将其转换为 long (Int64) 并使用此构造函数

DateTime date = new DateTime(long.Parse(logText));

回答by Fake Code Monkey Rashid

Assuming 'logText' is the ticks, try:

假设“logText”是刻度,请尝试:

DateTime logDate = new DateTime(logText);