locked
convert date string from yyyy/MM/dd format to datetime yyyy/MM/dd format RRS feed

  • Question

  • Hello guys.

    i want to convert this string: 1393/01/01 to exactly this: 1393/01/01 in datetime format.

    im using this code:

                string format = "yyyy/MM/dd";
                string persianDate = textBoxShamsi.Text;
    
                DateTime persianDateTime = DateTime.ParseExact(persianDate, format, CultureInfo.InvariantCulture);
                textBoxMiladi.Text = persianDateTime.ToString();

    but the output is like this:

    1/1/1393 12:00:00 AM

    thank you.

    Wednesday, June 10, 2015 8:01 AM

Answers

  • hello, this is what i get when i run the code bellow 

       private void button1_Click(object sender, EventArgs e)
            {
                string format = "1393/01/01";
                DateTime dt = Convert.ToDateTime(format);
                MessageBox.Show(dt.ToString("yyyy/MM/dd"));
             
               
            }
    

    i will search for a way to get your desired format "yyyy/MM/dd";


    Hope it helps. Spiri

    • Proposed as answer by FatherlyNick Wednesday, June 10, 2015 10:10 AM
    • Marked as answer by Kristin Xie Friday, June 19, 2015 8:45 AM
    Wednesday, June 10, 2015 9:25 AM
  • Use the ToString overload that takes the format and an IFormatProvider:

          string format = "yyyy/MM/dd";
          string persianDate = "1393/01/01";
    
          DateTime persianDateTime = DateTime.ParseExact(persianDate, format, System.Globalization.CultureInfo.InvariantCulture);
          string formatted = persianDateTime.ToString(format, System.Globalization.CultureInfo.InvariantCulture); //= "1393/01/01"
    textBoxMiladi.Text = persianDateTime.ToString(format, System.Globalization.CultureInfo.InvariantCulture);


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Wednesday, June 10, 2015 1:39 PM

All replies

  • Hi.

    Try to use your format as parameter of .ToString() method in the last row.


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

    Please mark my answer if it helped you, I would greatly appreciate it.

    Wednesday, June 10, 2015 8:11 AM
  • hello, this is what i get when i run the code bellow 

       private void button1_Click(object sender, EventArgs e)
            {
                string format = "1393/01/01";
                DateTime dt = Convert.ToDateTime(format);
                MessageBox.Show(dt.ToString("yyyy/MM/dd"));
             
               
            }
    

    i will search for a way to get your desired format "yyyy/MM/dd";


    Hope it helps. Spiri

    • Proposed as answer by FatherlyNick Wednesday, June 10, 2015 10:10 AM
    • Marked as answer by Kristin Xie Friday, June 19, 2015 8:45 AM
    Wednesday, June 10, 2015 9:25 AM
  • Use the ToString overload that takes the format and an IFormatProvider:

          string format = "yyyy/MM/dd";
          string persianDate = "1393/01/01";
    
          DateTime persianDateTime = DateTime.ParseExact(persianDate, format, System.Globalization.CultureInfo.InvariantCulture);
          string formatted = persianDateTime.ToString(format, System.Globalization.CultureInfo.InvariantCulture); //= "1393/01/01"
    textBoxMiladi.Text = persianDateTime.ToString(format, System.Globalization.CultureInfo.InvariantCulture);


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Wednesday, June 10, 2015 1:39 PM