Solved

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

Posted on 2004-04-21
12
2,160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month5 days, 12 hours left to enroll

627 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