Convert month string to int format [duplicate] - c#

This question already has answers here:
How to parse a month name (string) to an integer for comparison in C#?
(15 answers)
Closed 8 years ago.
I have the following string "Dec".
How can I convert this string to the month number 12?
I tried this:
DateTime.ParseExact("Dec", "MMMM", null).Month
This doesn't work.

You can also use ParseExact method. i.e,
string month = "Dec";
int MonthDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;

Simply use "MMM":
DateTime.ParseExact("Dec", "MMM", null).Month
also best use a specific culture, so its consisten with the input:
DateTime.ParseExact("Dec", "MMM", System.Globalization.CultureInfo("en-us")).Month
The long version MMMM refers to a full month name as originaly posted here How to parse a month name (string) to an integer for comparison in C#?

Related

C#: DateTime form string [duplicate]

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
}

C# - String was not recognized as a valid DateTime [duplicate]

This question already has answers here:
c# parse date time with timezone
(3 answers)
Closed 3 years ago.
I'm getting the error "string not recognized as a valid DateTime". I've tried numerous formats but continue to get the error. I want to return "10/31/2019".
string x = DateTime.ParseExact(
"2019-10-31T09:40:28-04:00",
"yyyy-MM-ddTHH:mm:ss-K",
DateTimeFormatInfo.InvariantInfo).ToString("d");
The format string should be yyyy-MM-ddTHH:mm:ssK or yyyy-MM-ddTHH:mm:sszzz. notice the lack of - in the format string.
Not sure what the -K is doing, you need a zzz there.
"yyyy-MM-ddTHH:mm:sszzz" should be your formatter.
In your example the format for timezone is not -K. The minus character is part of the timezone (K format), not a separator between time and timezone. For example -04:00 means "minus four hours". So it should be:
string x = DateTime.ParseExact(
"2019-10-31T09:40:28-04:00",
"yyyy-MM-ddTHH:mm:ssK", // <-----
DateTimeFormatInfo.InvariantInfo).ToString("d");
The format string needs to modified as follows
string x = DateTime.ParseExact("2019-10-31T09:40:28-04:00", "yyyy-MM-ddTHH:mm:sszzz", DateTimeFormatInfo.InvariantInfo).ToString("d");
This worked.
string x = DateTime.ParseExact("2019-10-31T09:40:28-04:00", "yyyy-MM-ddTHH:mm:sszzz", DateTimeFormatInfo.InvariantInfo).ToString("d");

Convert string with date & UTC offset to DateTime [duplicate]

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

Making a DateTime? from two strings for date and time [duplicate]

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));

How can i subtract a datetimepicker from current date in C# .net? [duplicate]

This question already has answers here:
How to convert date format to DD-MM-YYYY in C#
(16 answers)
Closed 7 years ago.
TimeSpan result = DateTime.Today.Subtract(birthDaydateTimePicker.Value);
resultTextBox.Text = result.ToString();
I want the result to show as dd/mm/yyyy format.
This should work, your subtracting dates looks fine assuming your parameter is a date value
{
TimeSpan result = DateTime.Today.Subtract(birthDaydateTimePicker.Value);
resultTextBox.Text = result.ToString("dd/MM/yyyy");
}
Subtracting dates
Changing the format of a date

Categories