C# getting the date [duplicate] - c#

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
convert string to date without time
(3 answers)
Closed 5 years ago.
I'm converting a string to a date format using DateTime.Parse(). My original string consists of the date only but when i use DateTime.Parse() it adds the time to it as well giving me 01/12/2000 00:00:00. I only want the date 01/12/2000. Is there any other way to simply just get the date?

DateTime dt = DateTime.Parse("01/12/2000");
Console.WriteLine(dt.ToString("dd/MM/yyyy"));
DateTime always has a underlying Time fraction, it's just the way you define to show it that makes it look like that. So the tostring function can be used with the given formatting.
If you really want it not to have a time (and i wouldn't know why you'd want that) there are 3th party addons available.

Related

C# DATE TIME CONVERSIONS WITH TIME ZONES [duplicate]

This question already has answers here:
Given a DateTime object, how do I get an ISO 8601 date in string format?
(18 answers)
Closed 6 months ago.
Im consuming a REST api that returns a datetime in the format 2022-09-08T00:21:32.712+03:00 this translates to 08/09/2022 00:21. How do I convert a date value say 08/09/2022 00:21 to the format 2022-09-08T00:21:32.712+03:00 in C#? I have tried below cord without success
string NIRA_CREATEDDATE_FORMATED = NIRA_CREATEDDATE.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'");
That is an ISO 8601 format. C# will output it if you use the "o" format.
DateTime.Now.ToString("o")
Note that you'll likely see a higher level of precision than the one you've provided, but that won't typically matter.

Convert a formatted date/time string into a DateTime object for comparison? [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 5 years ago.
I'm writing an automation date and I want to extract a string, turn it into a DateTime object, and compare it with the current date and time.
The string in question has this format: 7/28/2017 1:17:29 PM
How can I convert it to a DateTime object to compare with the current time (Basically, my end goal is to verify that it is within a few minutes of the current time)
Use DateTime.Parse() or DateTime.TryParse()
System.Convert can convert to and from many types. For example...
int intElapsedMinutes = (DateTime.Now - Convert.ToDateTime("7/28/2017 1:17:29 PM")).TotalMinutes

Convert 24-hour string value to timespan [duplicate]

This question already has answers here:
Parse exact custom time format?
(2 answers)
Closed 5 years ago.
I'm reading data from an excel file. The user can specify the time in one of the following formats:
1600
16:00
In the latter case, I can easily convert it to a timespan. How do I convert the first format to timespan? Is there a straightforward way than manipulating the string to convert to the latter and do it?
Use
TimeSpan span = TimeSpan.ParseExact("1600", new string[] {"hhmm", #"hh\:mm"}, CultureInfo.InvariantCulture);

Splitting Date and specially Time in C#. How to get the AM/PM thing [duplicate]

This question already has answers here:
How do I get the AM/PM value from a DateTime?
(15 answers)
Closed 6 years ago.
I have the value as #7/13/2016 3:20:00 PM# And want to separate it out date and time. Format for date is "07/13/2016" and time is "03:20 PM". I have got the values with StartDateTime.ToString("MM/dd/yyyy") and StartDateTime.ToString("HH:mm") but i am not sure about the "AM or PM" thing.
If I understand correctly, you have a DateTime value that you are trying to break into separate Date and Time. Microsoft has great documentation on DateTime formatting but I think what you are looking for for your time element is this:
SomeDateTime.ToString("hh:mm tt");
which should output "03:20 PM" or whatever the case may be.

Convert string (ex. 08:00) to datetime c# [duplicate]

This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 6 years ago.
I'm using an api that provides time as a string, for example "14:45". Ultimately, I would like to convert that UTC time string to the user's local time zone, but I'm not exactly sure how to do this.
The only solution I could think of is to convert the string to a DateTime and then convert that DateTime to local before pushing that out as a string back in the original format (ex. "14:45" becomes "11:45"). I'm stuck trying to convert the string to a DateTime given that it's not in the typical format. Any suggestions? Thanks!
Alright I was able to figure it out using the following:
string time = "08:00";
var convertedTime = Convert.ToDateTime(time).ToLocalTime().TimeOfDay.ToString().Substring(0,5);

Categories