Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting current date in seconds?

Status
Not open for further replies.

dillwyn

Programmer
Jul 23, 2003
2
UA
Hello,

Probably I'm asking a stupid Q but I can't find answer on it, please help.

How do I get current date in seconds since YYYY ?
string now_time=DateTime.Now.Second.ToString(); does not seem to be working..

Thanks in advance!
 
Code:
  double origin = new DateTime(2003,1,1).ToOADate();
    // the date you are counting from
    // this will be expressed in DAYS.

  double now = DateTime.Now.ToOADate();
    // today's date -- or whatever your comparison date is.
    // also expressed in days.
			  
  double diff = Math.Abs(now - origin);
    // difference in days between both dates

  double diffInSeconds = diff * 86400.0;
    // conversion from days to SECONDS (24*60*60)

ToOADate() converts a DateTime to the old-fashioned OLE Automation date, which us old VB6 hacks could get by just using CLng(Now()). This will return the number of days from 12/30/1899. I used the absolute value function above only to remove the negative value if the origin date were in the future (or the comparison date prior to origin).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top