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.
Related
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"));
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.
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.
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.
This question already has answers here:
Convert String value format of YYYYMMDDHHMMSS to C# DateTime
(3 answers)
Closed 7 years ago.
I my application I need to convert a string in the datetime format as below:
var k= DateTime.Parse("20150129163809");
But its throwing up an error message that "String was not recognized as a valid DateTime."
But, when I do
DateTime.Parse("2015-01-29 16:38:09")
its working fine...
What could be wrong?
Use ParseExact to parse well-formatted strings:
var k = DateTime.ParseExact("20150129163809","yyyyMMddHHmmss",CultureInfo.InvariantCulture);
DateTime.Parse is fairly limited in its capabilities and does not try and "guess" what format the string is in.