Xamarin Forms Can not parse a DateTime [duplicate] - c#

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 1 year ago.
I am trying to get this date 4th April 2021 12:00 which is todays date into a DateTime object.
I am in the UK and the emulator's locale is set to GB summer time and the format looks correct in it's setting page.
I have a date as a string:
string dateTimeStr = "08-04-2021 12:00";
I have tried this:
t = DateTime.Parse(dateTimeStr);
I get a datetime object of
4th Aug 2021 12:00
How can I parse a date as a string to DateTime and be sure that the month and the day will not be swapped?

DateTime.ParseExact(dateTimeStr, "dd/MMM/yyyy hh:mm", CultureInfo.InvariantCulture);
you can use ParseExact and mention the date format as per your requirement.

Related

Parse Particular Date Format C# [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 1 year ago.
Can anybody help me convert this into date time format?
20210115122710
I need in format like
DateTime date = DateTime.ParseExact("20210115122710", "dd/MM/yyyy hh:mm:ss", null);
You are parsing DateTime with wrong format, use "yyyyMMddHHmmss" instead of "dd/MM/yyyy hh:mm:ss",
DateTime date = DateTime.ParseExact("20210115122710", "yyyyMMddHHmmss", null);
Now whenever you want to print it in given format, then use .ToString() method,
string dateInGivenFormat = date.ToString("dd/MM/yyyy HH:mm:ss");
Note: I used HH for hours instead of hh.
HH : Use of HH converts hours in 24-hours format i.e from 00 to 23.
hh : hh converts hours in 12-hours format i.e from 00 to 12.
If your time is in 12-hour format then use hh otherwise use HH.
Try it online

How to convert India Standard Time to SQL date and time in C# [duplicate]

This question already has answers here:
.NET DateTime to SqlDateTime Conversion
(6 answers)
Closed 6 years ago.
I want to convert below date and time format to SQL date and time
Thu Apr 07 2016 06:30:00 GMT+0530 (India Standard Time)
any one have idea Thanks.
Since your string has UTC Offset value, I would parse it to DateTimeOffset instead of DateTime since it can hold the offset part.
But neither DateTime nor DateTimeOffset keeps time zone information, you should use GMT and (India Standard Time) parts as a string literal delimiter.
var s = "Thu Apr 07 2016 06:30:00 GMT+0530 (India Standard Time)";
var dto = DateTimeOffset.ParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(India Standard Time)'",
CultureInfo.InvariantCulture);
Now you have a DateTimeOffset as {07.04.2016 06:30:00 +05:30}.
And I would insert this dto as datetimeoffset typed column in SQL Server (with a parameterized query of course) since it saves offset part as well.
+---------------------------+
| Time zone offset range |
+---------------------------+
| -14:00 through +14:00 |
+---------------------------+
If your input is a string, you will need to start parsing the date for the specific culture:
DateTime dt = DateTime.ParseExact(inputString, System.Globalization.CultureInfo("<your_culture>"));
where <your_culture> is one of the multiple culture names for your country (see http://www.csharp-examples.net/culture-names/)
then you can get the date back as an SQL-compatible string, with simple quotation marks included:
string sqlDate = dt.ToString("'yyyy-MM-dd HH:mm:ss'");

DateTime.date give me a full Date why? [duplicate]

This question already has answers here:
how to give format DateTime.Date?
(7 answers)
Closed 7 years ago.
I need only the date from a DateTime class so I use DateTime.Date,
but it gives me a Full DateTime format, like this:
DateTime date = new DateTime();
date.Date
give me:
+ date {12/01/2016 00:00:00} System.DateTime
I only need the:
date {12/01/2016}
12/01/2016 00:00:00 is the same as 12/01/2016 as a value.
If you wanna get 12/01/2016 as a string representation, you can use .ToString method like;
var str = date.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you wanna get this 12/01/2016 as a DateTime, simply you can't. A DateTime instance always has date and time part. That's why you see it as 12/01/2016 00:00:00 on debugger even if you set it's time part to midnight.

Datetime parse not working c# [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 7 years ago.
I am trying to read date from excel using code
String ss = (String)w.Cells[2 + i, 4].Value2;
dRow[3] = DateTime.Parse(ss);
Code works when ss = "12/11/2015" but gives error
String was not recognized as a valid DateTime
when ss = "13/11/2015"
It gives error because month can not be 12 but it is taking date as month. This is what I think. Same code is working on other PC. Do I need to check my date time format or anything like date setting.
DateTime.Parse uses standard date and time format of your CurrentCulture settings by default.
Looks like your CurrentCulture has MM/dd/yyyy format as a short date format and since there is no month as 13 in Gregorian Calendar (which probably uses as a Calendar for your CurrentCulture), you get FormatExcetion.
You can use DateTime.ParseExact method to specify your format exactly like;
dRow[3] = DateTime.ParseExact("13/11/2015", "dd/MM/yyyy",
CultureInfo.InvariantCulture);
If you get this as an input and you want to parse it to DateTime, you have to know which format it has. Other than that, it can generate ambiguous scenarios.
For example; what 01/02/2015 should be parsed as? 1st February or 2nd January?
You shouldn't assume that every string you supplied perfectly parsed with DateTime.Parse method. It is not that smart.

Parsing a specific string to a DateTime [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse string to DateTime in C#
I am getting date and time returned from an API as a string in the following format:
Mon Aug 13 15:04:51 -0400 2012
Does anyone have experience with how I can turn this into a DateTime?
How about
DateTime dt = DateTime.ParseExact("Mon Aug 13 15:04:51 -0400 2012",
"ddd MMM dd HH:mm:ss K yyyy",
CultureInfo.InvariantCulture)
You should read about Custom Date and Time Format Strings
Have a look at the DateTime.Parse method.
Or if you are not sure that the parsing will succeed, use the DateTime.TryParse method.
For unconventional date and time strings use the DateTime.ParseExact method.
Check these two links, I think they will be very useful to you:
string-format-datetime
Custom Date and Time Format Strings

Categories

Resources