How to Format Datetime to 24 Hours Time

Hour from DateTime? in 24 hours format

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

How to format DateTime to 24 hours time?

Use upper-case HH for 24h format:

String s = curr.ToString("HH:mm");

See DateTime.ToString Method.

Get 24 Hour time format in datetime variable

Use the lower-case h for your formats, test the following in LINQPad:

void Main()
{
CultureInfo provider = CultureInfo.InvariantCulture;
System.Globalization.DateTimeStyles style = DateTimeStyles.None;
string[] format = new string[] { "d/MMM/yy h:m:s tt","d-MMM-yy h:m:s tt" };
// ^ ^
// +------ here -------+
string strDate = "24-May-13 12:03:03 AM";

DateTime dt;
if (DateTime.TryParseExact(strDate, format, provider, style, out dt))
{
dt.Dump();
dt.Hour.Dump();
}
}

Outputs:

24.05.2013 00:03:03
0

DateTime Format like HH:mm 24 Hours without AM/PM

All DateTime objects must have a date and a time.

If you want just the time, use TimeSpan:

TimeSpan span = TimeSpan.Parse("16:20");

If you want a DateTime, add that time to the min value:

TimeSpan span = TimeSpan.Parse("16.20");
DateTime dt = DateTime.MinValue.Add(span);
// will get you 1/1/1900 4:20 PM which can be formatted with .ToString("HH:mm") for 24 hour formatting

Convert 12-hour format to 24-hour format in C#

Using extension methods are also good idea.

public static class MyExtensionClass
{
public static string ToFormat12h(this DateTime dt)
{
return dt.ToString("yyyy/MM/dd, hh:mm:ss tt");
}

public static string ToFormat24h(this DateTime dt)
{
return dt.ToString("yyyy/MM/dd, HH:mm:ss");
}
}

Then you can use these 2 methods as following:

var dtNow = DateTime.Now;

var h12Format = dtNow.ToFormat12h(); // "2016/05/22, 10:28:00 PM"
var h24Format = dtNow.ToFormat24h(); // "2016/05/22, 22:28:00"

converting date time to 24 hour format

Try this:

String dateStr = "Jul 27, 2011 8:35:29 AM";
DateFormat readFormat = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss aa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}

if (date != null) {
String formattedDate = writeFormat.format(date);
}

Display [datetime] object in 24 hour format

There isn't any reason you can't use Get-Date with the [datetime] object your code creates. For example:

$d = [datetime]::ParseExact(
'201809222130',
'yyyyMMddHHmm',
[System.Globalization.CultureInfo]::InvariantCulture
)

Get-Date $d -UFormat %R

You could also use the .ToShortTimeString() method:

$d.ToShortTimeString()

Or .ToString and specify the tokens:

$d.ToString('HH:mm')

Convert a 12 hour format to 24 hour format in sql server

Try this:

First convert varchar date to datetime and then you can manipulate it in the way you want as:

-- CONVERT TO DATETIME TO GET 24HR FORMAT
SELECT CONVERT(DATETIME, '10/1/2013 6:39:04 PM', 0)
-- Concatenate in required format
SELECT CONVERT(VARCHAR(10), CONVERT(DATETIME, '10/1/2013 6:39:04 PM', 0), 101)
+ ' '+ CONVERT(VARCHAR(5),CONVERT(DATETIME, '10/1/2013 6:39:04 PM', 0), 108)

Date and time in 24 hours format

Use H instead:

$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d H:i:s',strtotime($test));
echo $t;

H: 24-hour format of an hour with leading zeros 00 through 23

G should be the same, but without leading zeroes though. I suspect that your PHP is set to a different timezone than +0800. Can you confirm your timezone (date_default_timezone_get())?

EDIT

OP confirmed that his timezone was set to UTC, in which case it maskes perfect sense that it shows 7 in the morning, as date uses PHPs default timezone.

If you want to "inherit" the Timezone, while getting more flexibility, you should switch to DateTime:

echo (new DateTime($test))->format("Y-m-d H:i:s");


Related Topics



Leave a reply



Submit