Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to differentiate date using trim.
suppose date is in a string named Date and saved like this 03/05/2012
i want to separate day, month and year and store them in different string.
How do i do that?
Posted

Use the Day[^], Month[^] and Year[^] properties to get these individual components from a date and put them into different strings.
 
Share this answer
 
Comments
sahabiswarup 5-Mar-12 7:05am    
date is saved in a string. how do i use that above function?
Using, DateTime properties (like Day, Month and Year) you can extract or trim the information as:

C#
DateTime value = new DateTime("03/05/2012");
String day = value.Day;
String month = value.MOnth;
String year = value.Year;
 
Share this answer
 
Comments
sahabiswarup 5-Mar-12 7:07am    
date is not static. so that how do i do that?
Manfred Rudolf Bihy 5-Mar-12 7:26am    
So use a string variable instead of a string literal to supply the DateTime constructor with a date string.
Where is the problem?
You can get day, month and year by using below given code. If your date is selected dynamically then store it in a string and convert it to DateTime as shown below. Also I have modified the code so that you can get exact name of month and week also.

static void Main(string[] args)
{
    string Date = "03/05/2012";
    DateTime meetingAppt = Convert.ToDateTime(Date);
    string day = meetingAppt.Day.ToString();
    string month = meetingAppt.Month.ToString();
    string year = meetingAppt.Year.ToString();


    string day1 = meetingAppt.ToString("dddd");
    string month1 = meetingAppt.ToString("MMMM");
    string year1 = meetingAppt.ToString("yyyy");
}
 
Share this answer
 
v2
Comments
sahabiswarup 5-Mar-12 7:07am    
date is selected dynamically and then i saved it into a string and want to separate.
check this

C#
string date="15/2/2011"; //save your dynamic date in date variable.
DateTime  datevalue=Convert.ToDateTime(date);
string day=datevalue.Day.ToString();
string month=datevalue.Month.ToString();
string year=datevalue.Year.ToString();
 
Share this answer
 
Comments
Andy411 5-Mar-12 9:07am    
Take care of the different date formats, e.g MM/DD/YYYY vs. DD/MM/YYYY, if your app is for international use.

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