This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
how to convert string year to DateTime format ?
my code :
// click button
string a = "2014";
DateTime b = DateTime.Parse(a);
MessageBox.Show(b.ToString());
You can try something like this
var year = int.Parse("2014");
DateTime dateTime = new DateTime(year, 1, 1);
Console.WriteLine(dateTime.ToString());
Related
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;
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);
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
This question already has answers here:
How to subtract one year from DateTime [closed]
(4 answers)
Closed 8 years ago.
I am trying to find a date (string) that is 5 years from current date in the past:-
DateTime.UtcNow.Date - DateString = 5 years
Can anyone help me formulate this into c# syntax?
var myDate = DateTime.UtcNow;
var newDate = myDate.AddYears(-5);
You can add or subtract a TimeSpan from a DateTime object to get another DateTime object.
var fiveYearsAgo = DateTime.Now - TimeSpan.FromYears(5);
var fiveYearsFromNow = DateTime.Now + TimeSpan.FromYears(5);
This question already has answers here:
No DateTime?.ToString(string) overload?
(3 answers)
Closed 8 years ago.
I want to convert Nullable DataTime to string.
DateTime datetime = DateTime.Now;
string strdatetime = datetime.ToString("MM/dd/yyyy");
Above coding working fine, non nullable DateTime.
DateTime? datetime = DateTime.Now;
string strdatetime = datetime.ToString("MM/dd/yyyy");
This one showing error No overload for method 'ToString' takes 1 arguments.
try this one,
string strdatetime = datetime.HasValue ? datetime.Value.ToString("MM/dd/yyyy") : string.Empty;