String DDMMYYYYHHNN to DateTime

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hello all,

Is there a default or easy way in C# to convert a string which has this
format DDMMYYYYHHNN convert to a DateTime ?

Thanks,

Peter.
 
This code snippet should work for you:

DateTime date = DateTime.ParseExact(t, "ddMMyyyyHHmm",
System.Globalization.CultureInfo.InvariantCulture)
 
Is there a default or easy way in C# to convert a string which has this
format DDMMYYYYHHNN convert to a DateTime ?

DateTime d = DateTime.ParseExact(input, "ddMMyyyyhhmm", null);




Mattias
 
Thanks Sergey !

It works but with a little strange result - a little example to show : (see
inside the code)
Is there a explanation for this strange result ?

Regards,
Peter.
private void button1_Click(object sender, System.EventArgs e)

{

SetSysDateTime(textBox1.Text); // textBox1 has the value "190720051255"

}

public struct SystemTime

{

public ushort Year;

public ushort Month;

public ushort DayOfWeek;

public ushort Day;

public ushort Hour;

public ushort Minute;

public ushort Second;

public ushort Millisecond;

};

[DllImport("coredll.dll", EntryPoint="SetSystemTime",

SetLastError=true)]

public extern static bool WinCESetSystemTime(ref SystemTime st);



public bool SetSysDateTime(string MyDateTime)

{

bool result = false;



if (MyDateTime == string.Empty)

return result;

try

{

SystemTime st = new SystemTime();

// this line resulting in a sytem datetime of 02:55 ?

// DateTime dt = DateTime.ParseExact(MyDateTime,
"ddMMyyyyhhmm",System.Globalization.CultureInfo.CurrentUICulture);

//

//however next lines sets exact the system datetime according to the value
in textbox1

MyDateTime = MyDateTime.Substring(0,2) + "-" + MyDateTime.Substring(2,2) +
"-" + MyDateTime.Substring(4,4) + " " +

MyDateTime.Substring(8,2) + ":" + MyDateTime.Substring(10,2);


DateTime dt = System.Convert.ToDateTime(MyDateTime).ToUniversalTime();


st.Year = (ushort)dt.Year;

st.Month = (ushort)dt.Month;

st.Day = (ushort)dt.Day;

st.DayOfWeek = (ushort)dt.DayOfWeek;



st.Hour = (ushort)dt.Hour;

st.Minute = (ushort)dt.Minute;

st.Second = (ushort)dt.Second;

st.Millisecond = (ushort)dt.Millisecond;

result = WinCESetSystemTime(ref st);

if (true != result)

{

int error = Marshal.GetLastWin32Error();

MessageBox.Show(String.Format("SetSystemTime failed:{0:X8}", error));

}

}

catch (Exception x)

{

MessageBox.Show(x.ToString() + " Error (SetSysDateTime)");

result = false;

}

return result;

}



Sergey Bogdanov said:
This code snippet should work for you:

DateTime date = DateTime.ParseExact(t, "ddMMyyyyHHmm",
System.Globalization.CultureInfo.InvariantCulture)


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com

Hello all,

Is there a default or easy way in C# to convert a string which has this
format DDMMYYYYHHNN convert to a DateTime ?

Thanks,

Peter.
 
It should work with the ParseExact method

DateTime mydatetime =
Datetime.ParseExact(sourcestring,"ddMMyyyyyHHmm",Thread.CurrentThread.CurrentCulture)
I assume HHNN means hours and minutes in 24 hours style

Norbert
 
Thanks Norbert,

C# doesn't recognize Thread.CurrentThread.CurrentCulture ?
Is this not in the CF ? Or am I doing something wrong ?

Regards,
Peter.
 
Just tested it, the result for dt.Hour was 12, as expected. The only one
thing I've changed in your code:

"ddMMyyyyhhmm" => ddMMyyyyHHmm (see my previous post)

Also before pass that value to WinCESetSystemTime don't forget to call
ToUniversalTime.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com

Thanks Sergey !

It works but with a little strange result - a little example to show : (see
inside the code)
Is there a explanation for this strange result ?

Regards,
Peter.
private void button1_Click(object sender, System.EventArgs e)

{

SetSysDateTime(textBox1.Text); // textBox1 has the value "190720051255"

}

public struct SystemTime

{

public ushort Year;

public ushort Month;

public ushort DayOfWeek;

public ushort Day;

public ushort Hour;

public ushort Minute;

public ushort Second;

public ushort Millisecond;

};

[DllImport("coredll.dll", EntryPoint="SetSystemTime",

SetLastError=true)]

public extern static bool WinCESetSystemTime(ref SystemTime st);



public bool SetSysDateTime(string MyDateTime)

{

bool result = false;



if (MyDateTime == string.Empty)

return result;

try

{

SystemTime st = new SystemTime();

// this line resulting in a sytem datetime of 02:55 ?

// DateTime dt = DateTime.ParseExact(MyDateTime,
"ddMMyyyyhhmm",System.Globalization.CultureInfo.CurrentUICulture);

//

//however next lines sets exact the system datetime according to the value
in textbox1

MyDateTime = MyDateTime.Substring(0,2) + "-" + MyDateTime.Substring(2,2) +
"-" + MyDateTime.Substring(4,4) + " " +

MyDateTime.Substring(8,2) + ":" + MyDateTime.Substring(10,2);


DateTime dt = System.Convert.ToDateTime(MyDateTime).ToUniversalTime();


st.Year = (ushort)dt.Year;

st.Month = (ushort)dt.Month;

st.Day = (ushort)dt.Day;

st.DayOfWeek = (ushort)dt.DayOfWeek;



st.Hour = (ushort)dt.Hour;

st.Minute = (ushort)dt.Minute;

st.Second = (ushort)dt.Second;

st.Millisecond = (ushort)dt.Millisecond;

result = WinCESetSystemTime(ref st);

if (true != result)

{

int error = Marshal.GetLastWin32Error();

MessageBox.Show(String.Format("SetSystemTime failed:{0:X8}", error));

}

}

catch (Exception x)

{

MessageBox.Show(x.ToString() + " Error (SetSysDateTime)");

result = false;

}

return result;

}



This code snippet should work for you:

DateTime date = DateTime.ParseExact(t, "ddMMyyyyHHmm",
System.Globalization.CultureInfo.InvariantCulture)


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com

Hello all,

Is there a default or easy way in C# to convert a string which has this
format DDMMYYYYHHNN convert to a DateTime ?

Thanks,

Peter.
 
Back
Top