user818700
user818700

Reputation:

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 e.g. "20121029".

I have a string that looks exactly like this:

2012-10-29T08:45:00.000

...Push in the right direction anyone?

Upvotes: 1

Views: 2116

Answers (8)

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>";
        }
    }
} }

Upvotes: 0

Lokesh Gupta
Lokesh Gupta

Reputation: 472

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);
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062865

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);

Upvotes: 2

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

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)

Upvotes: 2

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

Upvotes: 2

Michael Arnell
Michael Arnell

Reputation: 1028

Just use DateTime's Parse method...

var date = DateTime.Parse("2012-10-29T08:45:00.000");

Upvotes: 1

Paul Talbot
Paul Talbot

Reputation: 1603

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

Upvotes: 1

Christoffer
Christoffer

Reputation: 2125

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

Upvotes: 1

Related Questions