Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying this code to copy last part on string which contain year...
so i would be able to calculat age from this...


string DOB = "10/10/1992"
string Year = "";
int j=0;
for(int i = 5 ; i < 10 ; i ++)
{
year[j]=DOB[i];// error
j++;
}
Posted
Comments
Varsha Ramnani 15-Feb-14 7:11am    
are you using C#?
If yes you can use datetime variable for DOB and use variablename.Year to find year.
Ammar Shaukat 22-Apr-14 13:28pm    
right..

No - you can't do that, because strings are immutable, which measn that once a string has been created, you cannot change it in any way. If you want to extract part of a string, then you need to create a new string from the existing one:
C#
string DOB = "10/10/1992";
string year = DOB.Substring(6, 4);

Or better, parse the date into a DateTime, and then get the year from that:
C#
string DOB = "10/10/1992";
DateTime dob = DateTime.Parse(DOB);
string year = dob.Year.ToString();
This works more flexibly, when the user can enter dates as it copes with more date formats than an explicit substring.
 
Share this answer
 
C#
string DOB = "10/10/1992"
datetime DOB= datetime.parse(dob.tostring());

int year=DOB.year;
 
Share this answer
 
Comments
Ammar Shaukat 15-Feb-14 7:21am    
getting error....
above solution works well... thanku
King Fisher 15-Feb-14 7:35am    
whats your error
Ammar Shaukat 11-Mar-14 13:49pm    
done...
C#
Year = DOB.Split('/').Last();
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900