c# - 如何将时间戳转换为日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17317466/
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
c# - How to convert Timestamp to Date?
提问by user2500094
I'm getting timestamp from xml document.Now, I want to convert Timestamp to Date format(13-May-13)
我从 xml 文档中获取时间戳。现在,我想将时间戳转换为日期格式(13-May-13)
XmlNodeList cNodes = xncomment.SelectNodes("comment");
foreach (XmlNode node in cNodes)
{
//I'm getting this "1372061224000" in comment-date
string comment_date = node["creation-timestamp"].InnerText;
}
Any ideas? Thanks in advance.
有任何想法吗?提前致谢。
采纳答案by Teoman Soygul
Given that this looks like a Java timestamp, simply use below:
鉴于这看起来像一个 Java 时间戳,只需在下面使用:
var dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Math.Round(1372061224000 / 1000d)).ToLocalTime();
Console.WriteLine(dt); // Prints: 6/24/2013 10:07:04 AM
回答by user1501990
Maybe replace this :
也许替换这个:
foreach (XmlNode node in cNodes)
{
//I'm getting this "1372061224000" in comment-date
string comment_date = node["creation-timestamp"].InnerText;
}
by :
经过 :
foreach (XmlNode node in cNodes)
{
Datetime comment_date = new DateTime(Convert.ToInt32(node["creation-timestamp"]);
}
回答by user2500094
Thanks to all.
谢谢大家。
Finally I got the output
最后我得到了输出
foreach (XmlNode node in cNodes)
{
comment_timestamp = node["creation-timestamp"].InnerText;
DateTime comment_date1 = new DateTime(Convert.ToInt64(comment_timestamp));
comment_date = Convert.ToDateTime(comment_date1).ToString("dd-MMM-yy");
}
Output:
输出:
01-MAY-13
回答by Sapnandu
Finally i found how to convert time stampto Date& Dateto time stamp. I found some places in project people keep date as time stamp for get difference quickly. so in this case they use to keep the table column as Int or time stamp. now the problem is that in the application while showing the data, you need to convert it into date variable. So for that we can use the following code to convert time stampto Date
最后我找到了如何将时间戳转换为日期和日期到时间戳。我发现项目中的一些地方人们将日期作为时间戳记,以便快速获得差异。所以在这种情况下,他们使用将表列保留为 Int 或时间戳。现在的问题是,在显示数据的应用程序中,您需要将其转换为日期变量。因此,我们可以使用以下代码将时间戳转换为日期
int ts = 1451174400;
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(ts).ToLocalTime();
string formattedDate = dt.ToString("dd-MM-yyyy");
Now you can get any date format from this variable.
现在您可以从此变量中获取任何日期格式。
In the second case if you want to convert Dateto time stampthen check the following code.
在第二种情况下,如果要将日期转换为时间戳,请检查以下代码。
int ts = (dt.Ticks - 621356166000000000) / 10000000;
Where dt is the date time variable & holding a date value.
其中 dt 是日期时间变量并保存日期值。
回答by Ramil Aliyev
You can use FromUnixTimeSecondsmethod of DateTimeOffset
您可以使用DateTimeOffset 的FromUnixTimeSeconds方法
var dateTime1 = new DateTime(2020, 4, 5, 12, 15, 12);//05.04.2020 12:15:12
var timeStamp = new DateTimeOffset(dateTime1).ToUnixTimeSeconds(); //1586074512
If FromUnixTimeSeconds's result is not corresponding your original datetime, you can use LocalDateTime property of DateTimeOffset object.
如果 FromUnixTimeSeconds 的结果与您的原始日期时间不对应,您可以使用 DateTimeOffset 对象的 LocalDateTime 属性。
var dateTime2 = DateTimeOffset.FromUnixTimeSeconds(timeStamp).DateTime;//05.04.2020 08:15:12
var dateTime3 = DateTimeOffset.FromUnixTimeSeconds(timeStamp).LocalDateTime;//05.04.2020 12:15:12
For more information please visit this link DateTimeOffset.FromUnixTimeSeconds(Int64) Method
有关更多信息,请访问此链接DateTimeOffset.FromUnixTimeSeconds(Int64) 方法

