locked
How to convert a string to datetime RRS feed

  • Question

  • Hi..i am importing data from excel to database.Excel sheet contains column names like EMpID,EmpName,DOJ(Date),DateTime.i want to know how can i pass datetime values from excel to database.the date format in excel sheet is 22/01/2011,and datetime format is 22/01/2011 12:30.

    The end user will enter the date in excel sheet in the format the way he is used to.I want to know how can i convert it to datetime datatype of database so the database can accept the date and time format.

    Hope u understood my doubt.


    Thanks & Regards Nazima Mahmood
    Tuesday, August 2, 2011 8:38 AM

Answers

  • // Date strings are interpreted according to the current culture.
    // If the culture is en-US, this is interpreted as "January 8, 2008",
    // but if the user's computer is fr-FR, this is interpreted as "August 1, 2008"
    string date = "01/08/2008";
    DateTime dt = Convert.ToDateTime(date);      
    Console.WriteLine("Year: {0}, Month: {1}, Day: {2}", dt.Year, dt.Month, dt.Day);
    
    // Specify exactly how to interpret the string.
    IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
    
    // Alternate choice: If the string has been input by an end user, you might 
    // want to format it according to the current culture:
    // IFormatProvider culture = System.Threading.Thread.CurrentThread.CurrentCulture;
    DateTime dt2 = DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal);
    Console.WriteLine("Year: {0}, Month: {1}, Day {2}", dt2.Year, dt2.Month, dt2.Day);
    
    /* Output (assuming first culture is en-US and second is fr-FR):
      Year: 2008, Month: 1, Day: 8
      Year: 2008, Month: 8, Day 1
     */

    Mark Answered, if it solves your question
    Rohit Arora
    • Proposed as answer by Kris444 Tuesday, August 2, 2011 9:23 AM
    • Marked as answer by Martin_Xie Friday, August 19, 2011 11:48 AM
    Tuesday, August 2, 2011 8:43 AM
  • Hello Nazima

    DateTime dateTime = DateTime.ParseExact(@"22/01/2011 12:30", @"dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);

    Will convert the string you mentioned to a DateTime. :)


    • Proposed as answer by Kris444 Tuesday, August 2, 2011 9:23 AM
    • Marked as answer by Martin_Xie Friday, August 19, 2011 11:48 AM
    Tuesday, August 2, 2011 8:45 AM

All replies

  • // Date strings are interpreted according to the current culture.
    // If the culture is en-US, this is interpreted as "January 8, 2008",
    // but if the user's computer is fr-FR, this is interpreted as "August 1, 2008"
    string date = "01/08/2008";
    DateTime dt = Convert.ToDateTime(date);      
    Console.WriteLine("Year: {0}, Month: {1}, Day: {2}", dt.Year, dt.Month, dt.Day);
    
    // Specify exactly how to interpret the string.
    IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
    
    // Alternate choice: If the string has been input by an end user, you might 
    // want to format it according to the current culture:
    // IFormatProvider culture = System.Threading.Thread.CurrentThread.CurrentCulture;
    DateTime dt2 = DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal);
    Console.WriteLine("Year: {0}, Month: {1}, Day {2}", dt2.Year, dt2.Month, dt2.Day);
    
    /* Output (assuming first culture is en-US and second is fr-FR):
      Year: 2008, Month: 1, Day: 8
      Year: 2008, Month: 8, Day 1
     */

    Mark Answered, if it solves your question
    Rohit Arora
    • Proposed as answer by Kris444 Tuesday, August 2, 2011 9:23 AM
    • Marked as answer by Martin_Xie Friday, August 19, 2011 11:48 AM
    Tuesday, August 2, 2011 8:43 AM
  • Hello Nazima

    DateTime dateTime = DateTime.ParseExact(@"22/01/2011 12:30", @"dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);

    Will convert the string you mentioned to a DateTime. :)


    • Proposed as answer by Kris444 Tuesday, August 2, 2011 9:23 AM
    • Marked as answer by Martin_Xie Friday, August 19, 2011 11:48 AM
    Tuesday, August 2, 2011 8:45 AM
  • Hi

    You should be considering entering into database in single format...Since DataBase considers 4/5/2011 and 5/4/2011 same, though they are in same format of DD/MM/YYYY

    Consider 3rd option given by Arora and option given by Marcus


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Tuesday, August 2, 2011 9:23 AM