• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4502
  • Last Modified:

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
0
rito1
Asked:
rito1
  • 4
  • 2
  • 2
  • +2
5 Solutions
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Imran Javed ZiaCommented:
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
 
Pratima PharandeCommented:
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_joshiCommented:
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
 
kumaresan2011Commented:
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
 
kumaresan2011Commented:
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now