Can't remove hidden characters from string - c#

I'm trying to remove hidden characters from a string that represents a date time. I'm using .Net Fiddle and you can see the line that tries to ParseExact fails.
Here is a snippet. Please refer to the fiddle link for working code.
var dateTime = "2015-04-14 07:30:00 PM"; //<= this throws an error from some hidden char
dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", "");
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd HH:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");

The HH in the format string refers to the 24-hour clock hours, which doesn't work when using AM/PM in the format string for PM times.
Change HH to hh.

It's not an invisible character. Your use of HH conflicts with your use of tt. HH is 24 hour time, but you are using tt to interpret PM (12 hour time). Change it to hh and it works.
var dateTime = "2015-04-14 07:30:00 PM";
//dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", ""); <= not needed
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");

You need to change HH to hh.
yyyy-MM-dd hh:mm:ss tt

Related

converting an existing date to another date c#

Wanted to pass a string value of 8/26/2015 8:00:00 PM for conversion to
2015-08-26 20:00:00.000 in SQL.
But before I could do that,
string was not recognized as a valid datetime.
DateTime clock = Clockval.StartTime.Value.Add(Clock.StartTime.Value.TimeOfDay);
theStartTimeConvert = DateTime.ParseExact(clock.ToString(), "dd/MM/yyyy HH:mm:ss tt", null).ToString("MMM. dd, yyyy HH:mm:ss");
Did I pass in the wrong codes? And is there other alternatives to convert to 2015-08-26 20:00:00.000 from DateTime?
First of all the M custom format specifier represents the month as a number from 1 through 12. You use it to parse number 23.
Second, since your months and hours can have a single digit you should use M and H instead of MM and HH:
var str = "8/26/2015 8:00:00 PM";
var parsedDate = DateTime.ParseExact(str, "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
var formattedDate = parsedDate.ToString("MMM. dd, yyyy HH:mm:ss");
If format works fine with my str, but doesn't work with your Clock.StartTime.Value.ToString(); - I guess you can have issue, that was desribed here. To cut the long story, sometimes strings isn't what you think is it. There a lot of Unicode characters which don't appear in the debug representation. Try to work with this string for parsing:
var str = Clock.StartTime.Value.ToString();
var correctDateStr= new String(str
.ToCharArray()
.Where(c => c <= 255)
.ToArray());

How to converting in to time?

I am trying to convert to time from a string
My string are like this "11:45 AM" or "03:19 PM" and i am using
dateTime = DateTime.ParseExact("11:45 AM", "H:mm tt",
System.Globalization.CultureInfo.InvariantCulture);
Then it is getting converted but when i am passing
DateTime.ParseExact("3:19 PM", "H:mm tt",
System.Globalization.CultureInfo.InvariantCulture).ToString();
Getting error as
String was not recognized as a valid DateTime.
I cant understand why it is happening any one have idea then please help me
I would use h instead of H. H is for the 24hr fromat, h for the 12hr format.
DateTime.ParseExact("9:45 PM", "h:mm tt", System.Globalization.CultureInfo.InvariantCulture)
See the full list of format options here.
As you want to parse the 12 hr format and convert it to the 24 hr format then you can just use this
string dt = DateTime.ParseExact("3:19 PM", "h:mm tt",CultureInfo.InvariantCulture).ToString("HH:mm");;
Unfortunately, none of the answers are completely correct.
Ante meridiem and post meridiem belong to the 12-hour clock format. That's why you should never use 24-hour clock format specifiers if your string contains one of them.
That's why you need to use h or hh specifiers, not H or HH. Since your hour part can be with leading zeros, using hh specifier is the best option for both of your string types.
Using the hh:mm tt format will parse your strings successfully.
string s = "03:19 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "hh:mm tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 29.05.2015 15:19:00
}
and
string s = "11:45 AM";
DateTime dt;
if(DateTime.TryParseExact(s, "hh:mm tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 29.05.2015 11:45:00
}
Also, you mentioned the 3:19 PM string in your code example. Since the hour part is single digit, you need to use the h:mm tt format in that case.
Invariant culture requires two-digit hours.

Can't convert String to DateTIme

My Date Format : 12/30/2014 12:00:00 AM (MM/dd/yyyy) and I have a string rep as
String DateOfIssue = "12/30/2014 12:00:00 AM";
DateTime DOI = DateTime.ParseExact((DateOfIssue).Trim(), "MM/dd/yyyy HH:mm:ss tt", CultureInfo.GetCultureInfo("en-GB"));
but it is throwing exception. I have seen a lot of posts on SO but none is working. What am I doing wrong ? Please help and point my error. I have tried en-US also but that is also not working.
when I remove tt every thing works fine.
HH - is for 24 hours format, use hh instead:
DateTime DOI = DateTime.ParseExact((DateOfIssue).Trim(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.GetCultureInfo("en-GB"));

convert string to datetime format invalid System.Datetime

I have been trying many different solutions found here but none works. I want to convert the string to the format of dd/MM/yyyy
editField["ExpiryTime"] = "5/19/2011 12:00:00 AM";
DateTime dt = DateTime.ParseExact(editField["ExpiryTime"].ToString(), "dd/MM/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
But I always get an error of invalid System.DateTime. Pleaes help!
Use CultureInfo.InvariantCulture to avoid culture issues like invalid date separators and this format:
M/dd/yyyy hh:mm:ss tt
Uppercase M is for months, dd are the days, yyyy the four digit years. Lowercase hh are the hours in 12h format(required in combination with AM/PM), mm are the minutes, ss the seconds and tt the AM/PM designator.
string input = editField["ExpiryTime"].ToString(); // "5/19/2011 12:00:00 AM"
DateTime dt = DateTime.ParseExact(input, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
I want to convert the string to the format of dd/MM/yyyy
Then use ToString in the same way, CultureInfo.InvariantCulture forces / as date separator, without it will be replaced with your current culture's date-separator:
string result = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you need it as string, then you should try this
var dt = string.Format("{0:dd/MM/yyyy}",DateTime.Now);
Note: Also check your local system date time format. If it mismatches with the used one , still you might experience the same exception..

DateTime parse with custom format

I am trying to parse a datetime value using this:
DateTime insertedDateTime = DateTime.ParseExact(tokens[0] + " " + tokens[1], "yyyy-MM-dd mm:hh:ss", CultureInfo.InvariantCulture);
//tokens[0] = 2013-09-05
//tokens[1] = 07:23:32
I am getting this error:
String was not recognized as a valid DateTime.
Any help would be appreciated.
you should write:
DateTime insertedDateTime = DateTime.ParseExact(tokens[0] + " " + tokens[1], "yyyy-MM-dd mm:HH:ss", CultureInfo.InvariantCulture);
because hh means 12h time and HH means 24h time and putting 23 as hour in 12h time is invalid :)
Of course if you are sure that hours are second in your time and you don't want to write HH:mm:ss or hh:mm:ss (for 12h format)
DEMO here
Hours should go first: "yyyy-MM-dd hh:mm:ss"
NOTE: Consider to use 24-hour HH format instead of 12-hour hh format.
You should change your mm:hh:ss to hh:mm:ss because you giving string hour part first.
DateTime insertedDateTime = DateTime.ParseExact(2013-09-05 07:23:32, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(insertedDateTime);
Output will be;
9/5/2013 7:23:32 AM
Here a DEMO.
For more informations;
Custom Date and Time Format Strings
try to use
string strdate= "yourdate";
DateTime.ParseExact(strdate, "M/d/yyyy hh:mm", CultureInfo.InvariantCulture);

Categories