This question already has answers here:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
(13 answers)
datetime.parse and making it work with a specific format
(2 answers)
Closed 2 years ago.
So I have this `string:
string time = "20201006 10:42:42.925"
Which look like valid time format.
And this is what I have try:
DateTime dt1 = DateTime.Parse(time);
And got this error: String was not recognized as a valid DateTime.
Use TryParse Method, eg
dateTime dt1;
if DateTime.TryParse(time, out dt1)
{
// all good
} else{
// no good
}
Related
This question already has answers here:
Given a DateTime object, how do I get an ISO 8601 date in string format?
(18 answers)
Closed 4 years ago.
I am attempting to query table storage by generating the following query:
var date = new DateTime(1954, 9, 7);
var timequery = TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThanOrEqual, date.ToString());
I am getting a bad request when simply doing date.ToString()
The string that I need would be in the following format: 1954-09-07T07:00:00.0000000Z
How do I convert a regular DateTime to be a string in the specified format?
Var date = new DateTime(1954,7,0,0,0,0,DateTimeKind.Utc);
Var stringDate = date.ToUniversalTime().ToString(“o”);
Gives you the required result as
1954-09-07T00:00:00.0000000Z
This question already has answers here:
How to create a .NET DateTime from ISO 8601 format
(7 answers)
Convert datetime without timezone
(4 answers)
Closed 4 years ago.
I have tried following way but doesn't work
dateTime="2018-12-13T07:33:35.893Z"
DateTime dt;
DateTime.TryParseExact(dateTime, out dt);
But I am always getting dt as {1/1/0001 12:00:00 AM}.
Can you please tell me why? and how can I convert that string to date?
I also tried Convert.ToDateTime but doesn't work.
What I actually want is getting the dd/MM/yyyy string'd DateTime so I could perform a query on a DB.
Have you got the original DateTime object or you simply have it in a string?
In case you've got it as DateTime:
string european = dateTime.ToString("dd/MM/yyyy");
In case you've got it as a string:
string date = "2018-12-13T07:33:35.893Z";
if(DateTime.TryParse(date , out DateTime result))
result.ToString("dd/MM/yyyy");
Have a look at the original MSDN documentation about the DateTime.ToString method
Since you've got a DateTime you can convert to that format:
var thisExactMoment = DateTime.Now;
thisExactMoment.ToString("dd/MM/yyyy");
With your "dateTime" variable, just perform dateTime.ToString("dd/MM/yyyy") and you're ready to go.
var dateTime = "2018-12-13T07:33:35.893Z";
var x = DateTime.Parse(dateTime).ToString(#"MM\/dd\/yyyy");
This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 6 years ago.
I am trying to parse a string "20160918000500 +0200" to DateTime containing offset value "+0200".
I tried the following but it gives invalid DateTime exception.
DateTime dtDateTime = DateTime.Parse("20160918000500 +0200",new CultureInfo("yyyyMMddHHmmss zzz"));
Is there a way to convert the String exactly to Datetime with UTC offset value?
To preserve your Offset, use the DateTimeOffset.ParseExact method:
string str = "20160918000500 +0200";
var result = DateTimeOffset.ParseExact(str, "yyyyMMddHHmmss zzz", CultureInfo.InvariantCulture);
Console.WriteLine(result);
I would suggest to try out one of ParseExact methods of DateTime class
This question already has answers here:
Converting a string to DateTime object
(5 answers)
Closed 7 years ago.
I have a string which value is "2016-01-07 20:43:01,803".
I'd like to convert it use DateTime.Parse method. it is failed.
How to convert to datetime with this type of string?
You could use the ParseExact method.
var input = "2016-01-07 20:43:01,803";
DateTime dt = DateTime.ParseExact(input, "yyyy-MM-dd HH:mm:ss,fff", CultureInfo.InvariantCulture);
Try to use DateTime.ParseExact with the right format. (the last phrase is very very important: right format)
DateTime dt = DateTime.ParseExact("2016-01-07 20:43:01,803", "yyyy-MM-dd HH:mm:ss,fff", null);
This question already has answers here:
How to combine two strings (date and time) to a single DateTime
(9 answers)
Closed 7 years ago.
My input I recieve two strings:
string1 = "12/15/2015"
string2 = "22:45"
How can I combine them to have a DateTime? value.
Use this.
DateTime dt = Convert.ToDateTime(string.Format("{0} {1}",string1,string2));