How to Split Date into Day, Month Year String Variables

Hi All,

I have the following date - 25/12/2012 and what I need to do is split the date into 3 string variables strDay, strMonth and strYear.

Please could you provide me with the syntax to achieve this?

Many thanks,

Rit
LVL 1
rito1Asked:
Who is Participating?
 
Imran Javed ZiaConnect With a Mentor Consultant Software Engineer - .NET ArchitectCommented:
Hi,
Please use following code
string date = "25/12/2012";
            string [] dateTokens = date.Split('/');

            string strDay = dateTokens[0];
            string strMonth = dateTokens[1];
            string strYear = dateTokens[2];

Thanks
0
 
mayank_joshiCommented:
select DATEPART(DD,GETDATE()),DATEPART(MM,GETDATE()),DATEPART(YYYY,GETDATE())

Open in new window


replace getdate() with your date.
0
 
mayank_joshiCommented:
select DATEPART(DD,convert(datetime,'25/12/2012',103))
select DATEPART(MM,convert(datetime,'25/12/2012',103))
select DATEPART(YYYY,convert(datetime,'25/12/2012',103))
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
mayank_joshiCommented:
you may also use:-

select DAY(convert(datetime,'25/12/2012',103))
select MONTH(convert(datetime,'25/12/2012',103))
select YEAR(convert(datetime,'25/12/2012',103))

Open in new window

0
 
Pratima PharandeConnect With a Mentor Commented:
DateTime date = Convert.ToDateTime( "25/12/2012");
            
                  string strDay = date.Day.ToString();
                  string strMonth =  date.Month.ToString();
                  string strYear = date.Year.ToString();
0
 
Pratima PharandeCommented:
     DateTime date = Convert.ToDateTime( "12/25/2012");
            
                  string strDay = date.Day.ToString();
                  string strMonth =  date.Month.ToString();
                  string strYear = date.Year.ToString();
0
 
mayank_joshiConnect With a Mentor Commented:
sorry i thought it was sql server.
 for C#  you can use below:-

DateTime dt = Convert.ToDateTime("25/10/2010");
string strDay = dt.Day;
string strMonth = dt.Month;
string strYear = dt.Year;

Open in new window

0
 
kumaresan2011Connect With a Mentor Commented:
Hi

use this c# codings
             DateTime moment = Convert DateTime("13-05-2011 16:35:22:11");
                  // Year gets 2011.
                  int year = moment.Year;
                  
                  // Month gets 5 (MAY).
                  int month = moment.Month;
                  
                  // Day gets 13.
                  int day = moment.Day;
                  
                  // Hour gets 4.
                  int hour = moment.Hour;
                  
                  // Minute gets 35.
                  int minute = moment.Minute;
                  
                  // Second gets 22.
                  int second = moment.Second;
                  
                  // Millisecond gets 11.
                  int millisecond = moment.Millisecond;

i hope this will helpful to u

by
kumaresan.
0
 
kumaresan2011Connect With a Mentor Commented:
DateTime moment = NEW DateTime("13-05-2011 16:35:22:11");
                  // Year gets 2011.
                  int year = moment.Year;
                 
                  // Month gets 5 (MAY).
                  int month = moment.Month;
                 
                  // Day gets 13.
                  int day = moment.Day;
                 
                  // Hour gets 4.
                  int hour = moment.Hour;
                 
                  // Minute gets 35.
                  int minute = moment.Minute;
                 
                  // Second gets 22.
                  int second = moment.Second;
                 
                  // Millisecond gets 11.
                  int millisecond = moment.Millisecond;
0
 
rito1Author Commented:
Excellent thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.