Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi friends!

i fetched date from database. in the database date is store as text(string) now i want to use this text and only take day and month from this text.

database date like this "24/12/2013"

now want only month and day from it how can i do i c#?

i used many things like below :

C#
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB")ds1.Ta;
DateTime datadate = new DateTime(Convert.ToInt32(bles[0].Rows[0]"birthdate"].ToString().Substring(2, 2)));


2.
C#
DateTime datadate = new DateTime(Convert.ToDateTime(ds1.Tables[0].Rows[0]["birthdate"].ToString(), "MM/DD/YY HH:MM:SS TT", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);


3.

C#
 string bdate = ds1.Tables[0].Rows[0]["birthdate"].ToString();
DateTime dt = Convert.ToDateTime(bdate); 


and used more method to do it but not succeed!

plz help me!
Posted
Updated 24-Dec-13 1:14am
v4
Comments
Richard MacCutchan 24-Dec-13 4:37am    
It is very bad practice to store dates as text, use a proper Date or DateTime field.
Gaurav Makwana 24-Dec-13 4:47am    
sorry sir but you know sometime its needed just think about it!
Richard MacCutchan 24-Dec-13 4:59am    
I have thought about and it is never needed. Everyone who does this runs into trouble sooner or later. Look at the code you have shown above, you have to convert a value to a string in order to convert it back to a value. How much wasted code and time is that adding to your application?
Gaurav Makwana 24-Dec-13 5:30am    
thnk you

TRY

DateTime.ParseExact(dateString, MMMM dd", System.Globalization.CultureInfo.InvariantCulture);
 
Share this answer
 
v2
Comments
Gaurav Makwana 24-Dec-13 5:28am    
thank you sir
JoCodes 24-Dec-13 9:15am    
Welcome :)
HI Try this..


C#
var datetime = DateTime.ParseExact("24/12/2013", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    string month  = datetime.ToString("MM"); // 12
      string day  = datetime.ToString("dd"); //24
 
Share this answer
 
try this and modify textbox.text with your dataset:-
C#
string strDay = Convert.ToDateTime(txtDt.Text).ToString("dd");
 string Month = Convert.ToDateTime(txtDt.Text).ToString("MM");
 
Share this answer
 
C#
String MMdd = DateTime.ParseExact("24/12/2013", "dd/MM/yyyy", null).ToString("MMdd");
 
Share this answer
 
v2
 
Share this answer
 
Use convert or parse to get the required output.

C#
DateTime.Parse(inputDate.Value).ToString("dd-MM-yyyy");
 
Share this answer
 
Comments
Gaurav Makwana 24-Dec-13 4:47am    
it is not working sir!
Ganesh Raja 24-Dec-13 4:51am    
Can u pls check the below..and its working.
string temp = DateTime.Parse("24/12/2013").ToString("dd-MM");
Gaurav Makwana 24-Dec-13 5:30am    
thank you but it is also not working ganesh sir
its says date in not in valid datetime date
Add this function to your code

C#
private DateTime ParseDate(string input)
        {
            //Date formats, if your date is not in this format then please adds that 
            //custom format to this array, so it will convert your string to appropriate date
            // like 24-Dec-2013 is my string of date and it’s in dd-MMM-yyyy format and it’s not in current 
            //listed format then I just need to add dd-MMM-yyyy to below format lists, in the same way 
            //you need to add your fomat to this list to get sucessfully convertion of string to datetime

            string[] formats = new string[] { "MM/dd/yyyy"
                    , "MM/dd/yyyy HH:mm:ss tt"
                    , "MM/dd/yyyy HH:mm:ss"
                    , "dd/MM/yyyy"
                    , "dd/MM/yyyy HH:mm:ss tt"
                    , "dd/MM/yyyy hh:mm:ss"
                    , "dd/MM/yyyy hh:mm:ss tt"
                    , "dd/MM/yyyy HH:mm:ss" };

            DateTime result = default(DateTime);
            DateTime.TryParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
            
            return result;
        }


and use this function like this..
C#
DateTime BirthDateTime = ParseDate("25/06/1988");
           int BirhtMonth = BirthDateTime.Month;
           int BirhtDay = BirthDateTime.Day;


in your case you need to pass like this..
C#
DateTime BirthDateTime = ParseDate(ds1.Tables[0].Rows[0]["birthdate"].ToString());
            int BirhtMonth = BirthDateTime.Month;
            int BirhtDay = BirthDateTime.Day;
 
Share this answer
 
Comments
Gaurav Makwana 24-Dec-13 5:28am    
thank you sir
Tejas Vaishnav 24-Dec-13 5:30am    
If this solve your problem then mark it as your answer and also do not forgot to up vote, it will help other to solve their problem
you can use this


C#
string strDay = Convert.ToDateTime(txtDt.Text).ToString("dd");
 string Month = Convert.ToDateTime(txtDt.Text).ToString("MM");
 
Share this answer
 
Comments
TrushnaK 24-Dec-13 6:16am    
what you have done copy- paste
at least change variable name and textbox name...

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