Solved

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

Posted on 2004-04-21
12
1,849 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now