Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I got date from usb modem using at command.

and the formate is that "
Quote:
17/03/22,16:34:33+22
"

i want to convert string to datetime.

Thanks And Regards

What I have tried:

SMSRetPort.Open();

           SMSRetPort.Write("AT" + System.Environment.NewLine);
           Thread.Sleep(1000);

           //SMSRetPort.Write("AT+CTZU=1" + System.Environment.NewLine);
           //Thread.Sleep(1000);

           SMSRetPort.Write("AT+CMGF=1" + System.Environment.NewLine);
           Thread.Sleep(1000);

           SMSRetPort.Write("AT+CMGL=\"ALL\"\r" + System.Environment.NewLine);
           //SMSRetPort.Write("AT+CMGL=\"REC UNREAD\"\r" + System.Environment.NewLine); //"AT+CMGL=\"REC UNREAD\""
           Thread.Sleep(1000);
Posted
Updated 23-Mar-17 7:01am

C#
string s = "17/03/22,16:34:33+22";
            
s = s.Substring(0, s.IndexOf('+')); // remove the "+22"

DateTime dt;

DateTime.TryParseExact(s, "dd/MM/yy,HH:mm:ss", CultureInfo.CurrentCulture, DateTimeStyles.None, out dt);
 
Share this answer
 
Comments
Member 12104335 23-Mar-17 6:18am    
Error 1 'System.Windows.Forms.ColumnHeader' does not contain a definition for 'TryParseExact' and no extension method 'TryParseExact' accepting a first argument of type 'System.Windows.Forms.ColumnHeader' could be found (are you missing a using directive or an assembly reference?) D:\R&D\SMSapplication\SMSapplication\ReadSMS.cs 25 36 SMSapplication
F-ES Sitecore 23-Mar-17 6:46am    
You do TryParseExact on DateTime, not your ColumnHeader. Load your actual value into the "s" variable and the rest will be unchanged.
Because you have a known fixed format you can use the DateTime.ParseExact Method (String, String, IFormatProvider) (System)[^].

The format string for your dates might be:
// 17/03/22,16:34:33+22
//string format = "dd/MM/yy,HH:mm:sszz";
string format = "yy/MM/dd,HH:mm:sszz";

But your example string looks suspicious (22 for year and timezone).
[EDIT]
If that is a day offset, remove it like explained in solution 1 and remove the "zz" part from the format string.
[/EDIT]
 
Share this answer
 
v4
Comments
Member 12104335 23-Mar-17 6:20am    
could you please eleborate?
Jochen Arndt 23-Mar-17 6:24am    
Just follow the link and read. Regarding the format string there is a link to " Custom Date and Time Format Strings": https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Or do you refer to the "22".
That year is in the future and not a valid time zone offset.
Pete O'Hanlon 23-Mar-17 6:31am    
It's being read as YY/MM/DD so that's 22nd March 2017.
Jochen Arndt 23-Mar-17 6:38am    
Uups.
Thank you Pete.
Pete O'Hanlon 23-Mar-17 6:46am    
No problem.
To build on the answer from F-ES Sitecore, you can do this version if you don't want to split the string:
C#
public static void Main()
{
  string stringDateTime = "17/03/22,16:34:33+22";
  DateTime parsedDateTime;
  if (DateTime.TryParseExact(stringDateTime, "yy/MM/dd,HH:mm:ss+ff", CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal, out parsedDateTime))
  {
    Console.WriteLine(parsedDateTime);
  }
  Console.ReadKey();
}
This prints out 22/03/2017 16:34:33
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900