- Home /
Other
Day elapsed or day counter
hello everyone I would like to do a days count from the birth date "x" synchronized with the actual days that pass. The day of birth as an x = System.DateTime.Now; but I do not know how to compare the other days then to make sure that each time you increase one.
You can give me any tips?
Thank you
Answer by toddisarockstar · Nov 05, 2018 at 05:35 AM
I needed to do the same awhile ago and didnt find any shortcuts. thought I would share what i came up with. it works good for getting days passed between dates. It doesn't include leap year so once every 4 years, if the two dates are before and after the last leap the comparison is off. but it would be easy to add the adjustment if it matters to you.
public static int daysSince2000(string datetimestring){
int ret = 0;
string[] sa = datetimestring.Split ("/"[0]);
int d, m, y;
int.TryParse(sa [1],out d);
int.TryParse ( sa [0],out m);
int.TryParse (sa [2].Substring (2, 2),out y);
while(y>0){y--;ret+=365;}
while(m>0){m--;ret+=months[m];}
while(d>0){d--;ret+=1;}
return ret;
}
public static int[] months = new int[]{
31,//jan
28,//feb
31,//mar
30,//apr
31,//may
30,//jun
31,//jul
31,//aug
30,//sep
31,//oct
30,//nov
31,//dec
};
public void Start (){
string now = System.DateTime.Now.ToString ();
string FutureExample = "01/30/2019 11:19:01 PM";
int count1 = daysSince2000 (now);
int count2 = daysSince2000 (FutureExample);
print ("days passed between dates= " + (count2 - count1));
}
Answer by mustang4484 · Nov 23, 2018 at 08:58 AM
ok thank you, and if i would like to read only the number of the day from string?
Example today is 23/11/2018 and i would read only the "23" from system.datetime.now?
Thank you!!