Convert string(dd/MM/yyyy hh:mm) to datetime format [duplicate] - c#

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 8 years ago.
I have string with datetime format dd/MM/yyyy hh:mm.I want to calculate duration between two dates but failed to get datetime in correct format.please help.
thanks in advance

After parsing date string create two dates.
DateTime date1 = new DateTime();
DateTime date2 = new DateTime();
date1 = DateTime.Parse("22/05/2013 09:50:00");
date2 = DateTime.Parse("22/05/2014 09:50:00");
Then use TimeSpan structure to calculate interval:
TimeSpan ts_interval = date2 - date1;
You can use the following properties:
ts_interval.TotalSeconds;
ts_interval.TotalMinutes;
ts_interval.TotalHours;
For more visit http://msdn.microsoft.com/en-us/library/system.timespan_properties%28v=vs.110%29.aspx

you can use build in method
DateTime.Parse("12/05/1999 18:25");
you can also check this post

Related

How can i convert a string to date time in c#? [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 1 year ago.
I have a string returned from an api call .
The string is : 11/25/2021 05:20:21
Now that is a mm/dd/yyyy hh:mm:ss
I want to compare it to the datetime 2021-11-25 05:03:17.667
I do the following:
var dt1="11/25/2021 05:20:21";
var dt2= "2021-11-25 05:03:17.667"
DateTime date1 = Convert.ToDateTime(dt1);
DateTime date2 = Convert.ToDateTime(dt2);
int result = DateTime.Compare(date1, date2);
if (result < 0)
{
//d1 is earlier than d2.
}
but when i do the call DateTime date2 = Convert.ToDateTime(dt2); it gives an error , string is not in the correct format ?
You should probably use DateTime.ParseExact, which allows you to provide an excat format to parse your string.

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;

Get PM or AM from string date C# [duplicate]

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 4 years ago.
I have this string as a date and I want to add the AM/PM to the end.
From
10-17-2018 00:00:00
To
10-17-2018 00:00:00 AM
How is possible?
Thanks a lot!
So convert the string date into a real DateTime, then ToString() it back out in the format you like:
var dateStr = "10-17-2018 00:00:00";
var date = DateTime.Parse(dateStr);
Console.WriteLine(date.ToString("MM-dd-yyyy HH:mm:ss tt"));
Output: 10-17-2018 00:00:00 AM
DateTime formats: http://www.csharp-examples.net/string-format-datetime/

String was not recognized as a valid datetime - Datetime conversion issue [duplicate]

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 5 years ago.
I am having a date like "19/01/2018 15:30" as string.
I need to convert this to a DateTime object.
I am getting error:
String was not recognized as a valid datetime
This is my code:
DateTime drawDatetime = DateTime.ParseExact("19/01/2018 15:30"
, "dd/MM/yyyy hh:mm:ss", null);
You format string contains seconds but the date doesn't. Try this instead:
DateTime drawDatetime = DateTime.ParseExact(
"19/01/2018 15:30",
"dd/MM/yyyy HH:mm", null);

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

Categories

Resources