5

I am really new to C# and I am having problem in converting DateTime to it's Integer date format. I have search already the net but I did not find my answer.

I want is to convert the current Date to integer. Example the date "2011-08-11" in format "yyyy-MM-dd" has an integer value of 734360

Ronald Wildenberg
  • 31,634
  • 14
  • 90
  • 133
rechie
  • 2,139
  • 5
  • 25
  • 38

4 Answers4

12

myDateTime.Ticks will give you a uniquely representative Int64 value if that is what you need.

herzmeister
  • 11,101
  • 2
  • 41
  • 51
10

Pervasive uses days since 1/1/1.

To convert from int to a date use

new DateTime(1, 1, 1).AddDays(734360)

To convert to an int from a date use

TimeSpan t = (new DateTime(2011, 08, 11)-new DateTime(1, 1, 1));
int days = (int)t.TotalDays+1;
  • Be careful, when converting from the int format to the DateTime, don't forget to substract one to the number of days added (as 1/1/1 is the 1st day and not the 0th day). – Falanwe Aug 11 '11 at 11:24
  • @JobB yes your right Pervasive uses days since 1/1/1 that what my officemate told me. thank you so much! that solves the my problem. – rechie Aug 12 '11 at 02:28
5

You can try this too:

DateTime MyDate = new DateTime(); 

int Year = MyDate.Year;
int Month = MyDate.Month;
int Day = MyDate.Day;

if it is what you're looking for.

user
  • 86,916
  • 18
  • 197
  • 190
Derek
  • 51
  • 1
  • 1
-1
int calculation;
DateTime processDate;
calculation = Convert.ToInt32(Convert.ToString(processDate));

u can also use this way.

Ilaria
  • 167
  • 2
  • 13