Solved

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

Posted on 2004-04-21
12
1,806 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
Comment Utility
set also the timeformat
0
 
LVL 5

Author Comment

by:Darth_helge
Comment Utility
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
Comment Utility
function MyDateTimeToStr(Date: TDateTime): string; stdcall;
begin
  Result := FormatDateTime('dd.mm.yyyy HH:mm:ss', Date);
end;
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
Should be

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

Author Comment

by:Darth_helge
Comment Utility
esoftbg:

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

Accepted Solution

by:
kretzschmar earned 500 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Author Comment

by:Darth_helge
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
i have win2k
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

9 Experts available now in Live!

Get 1:1 Help Now