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:
  • 836 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hour from DateTime? in 24 hours format

#1
So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.
For example:
If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.

I'm working with Visual C#.
Any ideas please, thank you.

I have something like this

public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";

string retornar = "";
//here goes what i need
}
Reply

#2
date.ToString("HH:mm:ss"); // for 24hr format
date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM

Refer [this][1] link for other Formatters in DateTime.


[1]:

[To see links please register here]

Reply

#3
Using `ToString("HH:mm")` certainly gives you what you want as a *string*.

If you want the current hour/minute as *numbers*, string manipulation isn't necessary; you can use the `TimeOfDay` property:

TimeSpan timeOfDay = fechaHora.TimeOfDay;
int hour = timeOfDay.Hours;
int minute = timeOfDay.Minutes;
Reply

#4
Try this:

//String.Format("{0:HH:mm}", dt); // where dt is a DateTime variable

public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";

return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
}
Reply

#5
Try this, if your input is string
For example

string input= "13:01";
string[] arry = input.Split(':');
string timeinput = arry[0] + arry[1];
private string Convert24To12HourInEnglish(string timeinput)

{

DateTime startTime = new DateTime(2018, 1, 1, int.Parse(timeinput.Substring(0, 2)),
int.Parse(timeinput.Substring(2, 2)), 0);
return startTime.ToString("hh:mm tt");

}
out put: 01:01
Reply

#6
You can get the desired result with the code below. Two 'H' in `HH` is for 24-hour format.

return fechaHora.Value.ToString("HH:mm");
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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