Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 173 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert string to Time

#1
I have a time that is 16:23:01. I tried using `DateTime.ParseExact`, but it's not working.

Here is my code:

string Time = "16:23:01";
DateTime date = DateTime.ParseExact(Time, "hh:mm:ss tt", System.Globalization.CultureInfo.CurrentCulture);

lblClock.Text = date.ToString();

I want it to show in the label as 04:23:01 PM.
Reply

#2
string Time = "16:23:01";
DateTime date = DateTime.Parse(Time, System.Globalization.CultureInfo.CurrentCulture);

string t = date.ToString("HH:mm:ss tt");

Reply

#3
"16:23:01" doesn't match the pattern of "hh:mm:ss tt" - it doesn't have an am/pm designator, and 16 clearly isn't in a 12-hour clock. You're specifying that format in the *parsing* part, so you need to match the format of the existing data. You want:

DateTime dateTime = DateTime.ParseExact(time, "HH:mm:ss",
CultureInfo.InvariantCulture);

(Note the invariant culture, *not* the current culture - assuming your input genuinely always uses colons.)

If you want to *format* it to `hh:mm:ss tt`, then you need to put that part in the `ToString` call:

lblClock.Text = date.ToString("hh:mm:ss tt", CultureInfo.CurrentCulture);

Or better yet (IMO) use "whatever the long time pattern is for the culture":

lblClock.Text = date.ToString("T", CultureInfo.CurrentCulture);

Also note that `hh` is unusual; typically you *don't* want to 0-left-pad the number for numbers less than 10.

(Also consider using my [Noda Time][1] API, which has a `LocalTime` type - a more appropriate match for just a "time of day".)


[1]:

[To see links please register here]

Reply

#4
This gives you the needed results:

string time = "16:23:01";
var result = Convert.ToDateTime(time);
string test = result.ToString("hh:mm:ss tt", CultureInfo.CurrentCulture);
//This gives you "04:23:01 PM" string

You could also use `CultureInfo.CreateSpecificCulture("en-US")` as not all cultures will display AM/PM.
Reply

#5
The accepted solution doesn't cover edge cases.
I found the way to do this with 4KB script. Handle your input and convert a data.

Examples:
```
00:00:00 -> 00:00:00
12:01 -> 12:01:00
12 -> 12:00:00
25 -> 00:00:00
12:60:60 -> 12:00:00
1dg46 -> 14:06
```

You got the idea...
Check it

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through