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.