Click here to Skip to main content
11,630,144 members (80,337 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# time
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)
Posted 29-Oct-10 2:21am
Comments
chandras at 29-Oct-10 9:17am
   
Can u please clear me on the from where you got Timestamp:1287926608 (is it in the file ?)If so how does it get generated ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

date1.ToString("yyyyMMddHHmmssffff");
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


Advertise | Privacy | Mobile
Web04 | 2.8.150723.1 | Last Updated 29 Oct 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100