简体   繁体   中英

Convert this string to datetime object

I know that one can convert a string to a dateTime() object, but as far as I know the string needs to be in a particular form already eg "20121029".

I have a string that looks exactly like this:

2012-10-29T08:45:00.000

...Push in the right direction anyone?

With the T , that looks like the ISO date format (8601) commonly used by xml; consequently, XmlConvert exposes this very conveniently; try:

string s = "2012-10-29T08:45:00.000";
DateTime when = XmlConvert.ToDateTime(s);

The output is typical from DateTime structure, DateTime.parse("2012-10-29T08:45:00.000") , should solve the problem.

To know more about Date and Time Fromat String see this

Try using:

DateTime.ParseExact Method

Eg.

 string dateString = "2012-10-29T08:45:00.000";
 CultureInfo provider = CultureInfo.InvariantCulture;
 string format = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff";
 DateTime dt = DateTime.ParseExact(dateString, format, provider);

Or

DateTime.Parse Method 

DateTime.Parse(String)

There is a list of datetime standard formats: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

And you can always write a custom format: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Specifically, your format seems to match the "roundtrip" format: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#Roundtrip

只需使用DateTimeParse方法...

var date = DateTime.Parse("2012-10-29T08:45:00.000");
string strDt = "2012-10-29T08:45:00.000";
DateTime dt = DateTime.Parse (strDt);
string strDate = dt.ToString ("yyyyMMdd");

Below code will do all the work for you.

private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

public static void main(String[] args) throws ParseException {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);
    Date dateObj = dateFormatter.parse("2012-10-29T08:45:00.000");
    System.out.println(dateObj);
}

use this this might help

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

namespace DateTimeConvert { public 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>"; } } } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM