Solved

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

Posted on 2004-04-21
12
1,905 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Comparing dates in Delphi. Greater than/ Less than 3 153
How to debug For loops? 3 49
Delphi inherited method 6 69
Slow Restore if incremental backups using RDiff.exe 4 20
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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