Solved

date/time conversion from 12h AM/PM to 24 hours

Posted on 2004-04-21
12
1,959 Views
Last Modified: 2007-12-19
hi.

i have this function that will display the date right, regardless of what the locale settings are.
the thing is.. i can't get it right with the time section. it says 9:24 PM where is should say 21:24.
can anyone modify this function so this will be right?

function MyDateTimeToStr(Date: TDateTime): string; stdcall;
var
df: TFormatSettings;
begin
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT,df);
df.ShortDateFormat := 'dd.mm.yyyy';
Result := DateTimeToStr(Date,df);
end;
0
Comment
Question by:Darth_helge
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10875988
set also the timeformat
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 10876024
well, it's kind of embarrasing that i didn't see the shorttimeformat variable... but i add this:

df.ShortTimeFormat := 'hh:nn';
 but it doesn't work.

how should i use this?

0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10876052
function MyDateTimeToStr(Date: TDateTime): string; stdcall;
begin
  Result := FormatDateTime('dd.mm.yyyy HH:mm:ss', Date);
end;
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:mokule
ID: 10876085
Should be

df.ShortTimeFormat := 'hh:mm';
df.LongTimeFormat := 'hh:mm:ss';
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 10876096
esoftbg:

it still says 'AM' behind the datetime with your function.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 500 total points
ID: 10876106
use the longtimeformat

function MyDateTimeToStr(Date: TDateTime): string; stdcall;
var
  df: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT,df);
  df.ShortDateFormat := 'dd.mm.yyyy';
  df.LongTimeFormat := 'hh:nn';
  Result := DateTimeToStr(Date,df);
end;

meikl ;-)

0
 
LVL 5

Author Comment

by:Darth_helge
ID: 10876118
my function now looks like this:

function MyDateTimeToStr(Date: TDateTime): string; stdcall;
var
df: TFormatSettings;
begin
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT,df);
df.ShortDateFormat := 'dd.mm.yyyy';
df.ShortTimeFormat := 'hh:mm';
df.LongTimeFormat := 'hh:mm:ss';
Result := DateTimeToStr(Date,df);
end;

but the hour section is still 12h AM/PM.
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 10876152
kretzschmar, I love you.

I don't know what you look like, but I do love you.

thanks all for incredibly fast answers and good effort!
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10876153
Sorry, but under my Windows XP:

function MyDateTimeToStr(Date: TDateTime): string; stdcall;   ===> '21.04.2004 11:28:44'
begin
  Result := FormatDateTime('dd.mm.yyyy HH:mm:ss', Date);
end;

function MyDateTimeToStr(Date: TDateTime): string; stdcall;   ===> '21.04.2004 11:28:44 AM'
begin
  Result := FormatDateTime('dd.mm.yyyy hh:mm:ss AM/PM', Date);
end;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10876178
>I don't know what you look like, but I do love you.

seems i should provide a photo :-))

glad you got it work

meikl ;-)
0
 
LVL 17

Expert Comment

by:mokule
ID: 10876481
Is there anybody yet.

Will You please tell us on what system do You test it?

I've tried on Win XP Home

This gives 24 hour
df.LongTimeFormat := 'hh:mm:ss';
df.LongTimeFormat := 'hh:nn:ss';
df.LongTimeFormat := 'HH:mm:ss';
df.LongTimeFormat := 'HH:nn:ss';

This gives me 12 hour
df.LongTimeFormat := 'hh:mm:ss AM/PM';
df.LongTimeFormat := 'hh:nn:ss AM/PM';
df.LongTimeFormat := 'HH:mm:ss AM/PM';
df.LongTimeFormat := 'HH:nn:ss AM/PM';
0
 
LVL 5

Author Comment

by:Darth_helge
ID: 10876616
i have win2k
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question