C#时间戳 [英] C# Timestamp

查看:359
本文介绍了C#时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想编写一个程序,该程序必须将文件的最后写入时间转换为时间戳.
但是结果是错误的:
时间:24.10.2010 13:23:27(天,月,年小时,分钟,秒)
时间戳:1287926608

但这时间戳记是针对24.10.2010 15:23:28的,这是错误的.
Maby您有更好的解决方案,因为我想比较ftp在线文件和离线文件的日期.
我的想法是将时间转换为时间戳

DateTime date1 = new DateTime(1970, 1, 1);
TimeSpan ts = new TimeSpan(NextFile.LastWriteTime.Ticks - date1.Ticks);
Console.WriteLine(Convert.ToInt32(ts.TotalSeconds));




请帮我
安德烈
(很抱歉来自德国的我这个英语不好的人)

解决方案

对不起...您希望它输出什么?

因为您要告诉它输出从上次写入时间到1970年1月1日之间经过的总秒数.

为什么您完全搞砸了TimeSpans?

马克很接近...但是date1只是您选择的一个数字,因为我认为您认为这是一个时代.

它实际上不在.Net中.试试这个:

 DateTime date1 =  DateTime( 0 ); 


看看它给您什么日期...它将给您:0001年1月1日,午夜.

如果您想要这种类型的时间戳,请尝试以下操作:

 Console.WriteLine(NextFile.LastWriteTime.ToString(" )); 



仅供参考,总秒数= 14,906天13小时23分28秒. 1970年1月1日的儒略日数为2440588,而2010年10月24日的儒略数为2455494,相差14906天.

如果您只是想比较两个日期,就可以做

 如果(date1!= date2)


date1.ToString("yyyyMMddHHmmssffff");


Hi I want to write a program which have to convert the last written time of a file to a timestamp.
But the result is wrong:
Time: 24.10.2010 13:23:27 (Days, Month, Year Hours,Minutes,Seconds)
Timestamp: 1287926608

But this Timestamp is for 24.10.2010 15:23:28 and this is wrong.
Maby you have a better solution because I want to compare the date of a ftp online file and of a offline file.
My idea was to convert the time to a timestamp

DateTime date1 = new DateTime(1970, 1, 1);
TimeSpan ts = new TimeSpan(NextFile.LastWriteTime.Ticks - date1.Ticks);
Console.WriteLine(Convert.ToInt32(ts.TotalSeconds));




Pleas help me
Andre
(Sorry for this bad English Im from Germany)

解决方案

I''m sorry...what are you expecting it to output?

Because you are telling it to output the total seconds that have elapsed between the last written time and the January 1, 1970..

Why are you messing with TimeSpans at all?

Mark was close...but date1 is just a number you picked because, I assume, you thought it was the epoch.

It''s actually not in .Net. Try this:

DateTime date1 = new DateTime(0);


and see what date it gives you...it will give you: Jan 1, 0001, Midnight.

If you want that type of time stamp, just try this:

Console.WriteLine(NextFile.LastWriteTime.ToString("dd.MM.yyyy HH:mm:ss"));



FYI, that totalseconds = 14,906 days and 13 hours and 23 minutes and 28 seconds. The Julian Day Number for Jan 1, 1970 is 2440588 and the Day Number for Oct. 24, 2010 is 2455494 which is a difference of 14,906 days.

And if you''re just trying to compare two dates you can just do

if (date1 != date2)


date1.ToString("yyyyMMddHHmmssffff");


这篇关于C#时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆