C# Get Month INT from Month String [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 6 years ago.
I found a lot of ways to get the name from a number but now I need it the other way around.
If the string equals April, I want to convert it to int "4".
Does anyone know the best way to accomplish this server side?

int month = DateTime.ParseExact(MonthNameStr, "MMMM", CultureInfo.CurrentCulture ).Month
or you can do
int month = DateTimeFormatInfo.CurrentInfo.MonthNames.ToList().IndexOf(MonthNameStr) + 1;

int month = DateTime.ParseExact("April", "MMMM", CultureInfo.InvariantCulture).Month;

You can use the DateTime.ParseExact Method with a custom format specifier consisting only of the the "MMMM" Custom Format Specifier:
int month = DateTime.ParseExact("April", "MMMM", new CultureInfo("en-US"))
.Month;

Related

How to make formatException gone in Datetime [duplicate]

This question already has answers here:
Convert string to DateTime in c#
(3 answers)
DateTime.Parse throw exception in C#
(1 answer)
DateTime.Parse throwing format exception
(3 answers)
Closed 2 years ago.
Hi I just want to know how to make formatException gone when I'm inserting Datetime.
coding is in down below:
DateTime dob = Convert.ToDateTime(z);
dob.ToString("yyMMdd");
DateTime today = DateTime.Today;
today.ToString("yyMMdd");
var year = today.Year - dob.Year;
age = Convert.ToString(year);
return a;
Try use DateTime.TryParse or DateTime.TryParseExact to check your datetime string and store to your variable after parse at the same time
Use the DateTime.ParseExact() method to convert your date string z to a DateTime value -
var z = "23/09/2020";
DateTime dob = DateTime.ParseExact(z, "dd/MM/yyyy", null);
As you can see, you have to pass the date format you are trying to convert to the second parameter of the ParseExact() method as a string;

Convert Int to Time like 123430 to 12:34:30 [duplicate]

This question already has answers here:
Parsing Integer Value As Datetime
(5 answers)
Closed 6 years ago.
I would like to Convert Int to Time like 123430 to 12:34:30 in c#. Below code didnt work
DateTime dateTime = DateTime.ParseExact(timeint.ToString(), "HH:mm:ss",
CultureInfo.InvariantCulture);
The format string is the format that the inputted string is in. For example in your case HHmmss, just remove the colons and you should be fine.
Like:
DateTime dateTime = DateTime.ParseExact(timeint.ToString(), "HHmmss",
CultureInfo.InvariantCulture);

String format with c# [duplicate]

This question already has answers here:
Converting dd/mm/yyyy formatted string to Datetime [duplicate]
(3 answers)
Closed 7 years ago.
I have this string value
var d = "2016-01-07 09"
It's a string that contains a date with hour without minutes, seconds and millis.
I want to trasform into Datetime with this format
2016-01-07 09:00:00:000
I know that it sounds stupid but this stuff makes me crazy.
Thanks for support.
You have a Date with format Year - Month - Day Hour (yyyy-MM-dd HH).
You can use DateTime ParseExact method . If you know your dates format. You can parse it .
Check dateformats on this link
string d= "2016-01-08 03";
var c = DateTime.ParseExact(d, "yyyy-MM-dd HH", CultureInfo.InvariantCulture);
return c;

Convert string to datetime now format [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 8 years ago.
How I can convert string 23:59 to datetime format?
So that I can reduce the current time in it.
Example:
String = 23:59 minus
Datime.Now = 13.8.2014 10:59:55
Time left 12 hours 0 minutes.
i would use ParseExact
TimeSpan tsDifference = DateTime.ParseExact("23:59", "HH:mm", System.Globalization.CultureInfo.InvariantCulture) - DateTime.Now;
use this :
DateTime date = Convert.ToDateTime(string);
System.TimeSpan diff= DateTime.Now.Subtract(date);
diff will contain your deffernce

Age in years from DateTime (Date of birth) [duplicate]

This question already has answers here:
Closed 13 years ago.
Duplicate
How Can I calculate Someone's Age in C#?
I have a datetime variable that represents the date of birth of a user.
How can I get the age in years from this?
Update
I want a precise birthday, so 30.45 years or something.
Try the following (assuming the date of birth is stored in dtDOB):
public int getAgeInYears {
TimeSpan tsAge = DateTime.Now.Subtract(dtDOB);
return new DateTime(tsAge.Ticks).Year - 1;
}
Stolen from the answer to Jeff's question:
DateTime now = DateTime.Now;
int age = now.Year - bday.Year;
if (now < bday.AddYears(age)) age--;
You can try with (in Vb):
Dim dateOfBirth As Date
Now.Subtract(dateOfBirth).TotalDays \ 365
\ is an Integer division in Vb, I do not know if it has a correspondant in C#.

Categories

Resources