Convert this String to datetime C# [duplicate] - c#

This question already has answers here:
Datetime format Issue: String was not recognized as a valid DateTime
(8 answers)
Closed 5 years ago.
I tried converting this string to date time using ParseExact, but its giving exception on String format(String was not recognized as a valid DateTime)
Can someone please suggest some solution
DateTime dt = DateTime.ParseExact("2016-11-29T13:00:00", "yyyy-MM-dd'T'hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

You're parsing 24H time format. Use HH instead of hh for the hours.

Related

C# - Parse Process.StartTime to a certain format [duplicate]

This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
DateTime ParseExact string was not recognize as a DateTime C#
(2 answers)
Closed 4 months ago.
I can't figure a way to parse a Datetime object that is "10/24/2022 4:01:35 PM" to "24/10/2022 16:01:35".
Can't find an answer for this exact format.
Thanks in advance!
I guess you don't need to parse that string to DateTime since you have it already in Process.StartTime and you're looking at it in the debugger. Then use it directly. If you really have it as string you simply need to use DateTime.Parse with InvariantCulture:
DateTime dt = DateTime.Parse("10/24/2022 4:01:35 PM", System.Globalization.CultureInfo.InvariantCulture);
and for the output:
Console.Write(dt.ToString("dd'/'MM'/'yyyy HH:mm:ss"));

How to String date convert into datetime data type in C#? [duplicate]

This question already has answers here:
Converting string format to datetime in mm/dd/yyyy
(6 answers)
Closed 4 years ago.
I have todayDate variable which data type is DateTime and string variable dateString. i convert dateString value into DateTime Data type without Time.
string stringDate = "2018-05-07"
DateTime todayDate = Convert.ToDateTime(stringDate);
when I convert stringData into DateTime, todayDate value is "5/7/2018 12:00:00 AM". I need todayDate value is "2018-05-07" format only.
A DateTime object can be displayed with a format but a DateTime has no format. The format is specified when you try to convert a DateTime object to as String.
When you see your variable in Visual Studio, it is possible that your variable is displayed using american date format with a time part.
If you want to convert your date in yyyy-MM-dd format, you can use following code
string sFormattedDate = todayDate.ToString("yyyy-MM-dd");
The 12 hours notation in a little ambiguous for people that normally uses 24 hours notation because 12H AM are equal to 0H in 24 hours mode !

c# - how to convert 26/Jun/2016 at 13:14 to 2016-01-26 13:14:00 [duplicate]

This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
Closed 5 years ago.
i wanted to convert 26/Jun/2016 at 13:14 from a string to datetime data type
for saving to sql
string DtTime = ds.Tables["VOUCHER"].Rows[0]["BASICDATETIMEOFINVOICE"].ToString();
how to split as a string
You need to escape the / and the at in the format string, then you can use ParseExact:
DateTime.ParseExact("26/Jun/2016 at 13:14", "dd/MMM/yyyy 'at' HH:mm", CultureInfo.InvariantCulture);
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
But are you sure that the value is not stored as DateTime? Check by using ds.Tables["VOUCHER"].Rows[0].Field<DateTime>("BASICDATETIMEOFINVOICE"). Then no conversion from Object to string to DateTime is needed. If not you should consider to store it as DateTime in the first place, wherever the DataTable was filled from.

String yy-MM-dd HH:mm:ss.SSS convert to datetime in C# [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
How can convert this string yy-MM-dd HH:mm:ss.SSS to datetime in c#?
You can use the DateTime.ParseExact() method and define the exact format string that you are expecting to receive :
// This will parse a DateTime object using the "yy-MM-dd HH:mm:ss.fff" format
var date = DateTime.ParseExact(input,"yy-MM-dd HH:mm:ss.fff", null);
This assumes that the Y characters will represent a two-digit year and the S characters you were looking for would be considered fractional seconds, which have been converted to the y and f formats respectively.
Example
You can see a working example of this here.

How to accept datetime irrespective of format? [duplicate]

This question already has answers here:
Getting error : String was not recognized as a valid DateTime in c#
(3 answers)
Closed 8 years ago.
I have a web service created which will accept date time as input.
The data type i used is system.datetime.
It accepting if the input is in mm/dd/yyyy or yyyy/mm/dd but not if dd/mm/yyyy.
How to solve this?
You can use following logic
string str = "26/11/2014";
DateTime dt = DateTime.ParseExact(str, "dd/M/yyyy", CultureInfo.InvariantCulture);
newString = dt.ToString("MM/dd/yyyy");
Console.Write(newString);
Console.ReadKey();
and can pass newStr to WebService Method.

Categories