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

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

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.

How to ignore offset from string datetime [duplicate]

This question already has an answer here:
DateTime.ParseExact, Ignore the timezone
(1 answer)
Closed 4 years ago.
I have been searching to convert string to datetime with ignoring the offset.
I have a string with local date time as 2017-02-13T12:11:03.303 +01:00 i want to ignore the offset part and string should be converted to 2017-02-13 12:11:03.303 as datetime format. I searched google but could not find one.
Referred How to convert string to DateTime in C#?
Not a duplicate of DateTime.ParseExact, Ignore the timezone as while searching didn't know its realted to DateTimeOffset.Searched using layman terms and no proper result found.
Use the DateTimeOffset.Parse method:
string s = "2017-02-13T12:11:03.303 +01:00";
DateTime dt = DateTimeOffset.Parse(s, null).DateTime;

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

C# getting the date [duplicate]

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.

Categories