Highlighted
frand1 Super Contributor.
Super Contributor.
113 views

How to change DateTime time format to military time format (Visual Cobol)?

In a managed COBOL program, I am using DateTime to insert time stamps, but I don't want the time format of HH:MM:SS AM; I want it in military time format with no AM or PM.  I have not been able to find out how to do this on the site.

0 Likes
1 Reply
Micro Focus Expert
Micro Focus Expert

Re: How to change DateTime time format to military time format (Visual Cobol)?

"Managed COBOL" can mean COBOL for JVM or COBOL for .NET. When you ask a question about managed COBOL, please specify which variety you're talking about.

For any question, it's a good idea to include the product name, version, and platform as well. We're not psychic.

If you're talking about the .NET Framework's DateTime class (actually a structure, aka valuetype, not a class), that's a .NET Framework question, not a COBOL one. The answer is in the .NET Framework documentation:

https://docs.microsoft.com/en-us/dotnet/api/system.datetime

The Framework provides extensive support for formatting DateTime objects. You can format them directly to a string:

declare timeNow = type DateTime::Now
display timeNow::ToString("HH:mm:ss")

Note the "HH" specifier gives you the hours using a 24-hour clock and two digits.

Or you can specify a format for a DateTime object while doing composite formatting, e.g. with the String class Format method:

display string::Format("It's {0:HH:mm:ss}", type DateTime::Now)

There's a set of standard format specifiers, or you can use a custom format string as I've done here. Again, it's all in the .NET docs.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.