How to convert date and time string to DateTime data type?

The client sends a string containing the date in the format YYYYMMDDHHmmSS (for example, 201004224432 ). No delimiters such as / or - .

How can I easily convert this to a DateTime object? Convert.ToDateTime() does not work.

+4
source share
3 answers

Use DateTime.ParseExact :

 var date = DateTime.ParseExact( "201004224432", "yyyyMMddHHmmss", CultureInfo.InvariantCulture); 

Pay attention to the settings of your format string for proper operation.

+12
source

You want a DateTime.ParseExact that can take a format string like yours and use it to parse the input string.

+9
source

use this code example

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;

namespace DateTimeConvert {open partial class Form1: Form {public Form1 () {InitializeComponent (); }

  private void button1_Click(object sender, EventArgs e) { label1.Text= ConvDate_as_str(textBox1.Text); } public string ConvDate_as_str(string dateFormat) { try { char[] ch = dateFormat.ToCharArray(); string[] sps = dateFormat.Split(' '); string[] spd = sps[0].Split('.'); dateFormat = spd[0] + ":" + spd[1]+" "+sps[1]; DateTime dt = new DateTime(); dt = Convert.ToDateTime(dateFormat); return dt.Hour.ToString("00") + dt.Minute.ToString("00"); } catch (Exception ex) { return "Enter Correct Format like <5.12 pm>"; } } private void button2_Click(object sender, EventArgs e) { label2.Text = ConvDate_as_date(textBox2.Text); } public string ConvDate_as_date(string stringFormat) { try { string hour = stringFormat.Substring(0, 2); string min = stringFormat.Substring(2, 2); DateTime dt = new DateTime(); dt = Convert.ToDateTime(hour+":"+min); return String.Format("{0:t}", dt); ; } catch (Exception ex) { return "Please Enter Correct format like <0559>"; } } } } 
0
source

All Articles