Solved

Displaying the Time in a label

Posted on 2002-04-02
6
807 Views
Last Modified: 2012-08-13
I need to display the day of the week, the month and day followed by hour and minutes.  Everything is fine except for the first nine minutes of every hour.  This information is being formatted for display, and also saved to the registry for later displaying when the user needs to access this information.

This is what I have:
(Borland C++ Builder 4.5) -- Just listing needed info from function.  (xSaved is sent to the function, taken from the stored information from the registry.)

TDateTime xDate = xSaved;  // xSaved.DateTimeString();

char days[7][10] = "Sun.", "Mon.", "Tue.", "Wed.", "Thu.",
   "Fri.", "Sat." };

Word xYear, xMonth, xDay, xHour, xMin, xSec, xMSec;

DecodeDate(xDate, xYear, xMonth, xDay);
DecodeTime(xDate, xHour, xMin, xSec, xMSec);

String ampmStr;

String xTest = days[xDate.DayOfWeek() -1] + AnsiString(" ") + xMonth + "/" + xDay + "  " + xHour + ":" + xMin + " " + ampmStr;

// xTest displays in a label as Tue. 4/2 10:8 PM.  but after the first 9 minutes of the hour it displays as Tue. 4/2 10:10 PM.

I tried FormatString, but I can not get the hang of that.  Can someone help me out here?  Thanks.


0
Comment
Question by:LordDamein
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6915302
Why don't you test if xMin is < 10 and adjust it accordingly, e.g.

String xTest = days[xDate.DayOfWeek() -1] + AnsiString(" ") + xMonth + "/" + xDay + "  " + xHour + ":"
+ ((xMin > 9) ? ( xMin) : ("0" + xMin)) + " " + ampmStr;
0
 

Expert Comment

by:0xDEADBEEF
ID: 6915317
The easiest solution would be

String xTest;

if (xMin >= 10)
  xTest = days[xDate.DayOfWeek() -1] + AnsiString(" ") + xMonth + "/" + xDay + "  " + xHour + ":" + xMin + " " + ampmStr;
else
  xTest = days[xDate.DayOfWeek() -1] + AnsiString(" ") + xMonth + "/" + xDay + "  " + xHour + ":0" + xMin + " " + ampmStr;

Hope this helps.
0
 
LVL 1

Expert Comment

by:ilikenine
ID: 6915497
You surely want to call that overloaded '+' operator of String Class these many times ?

Well, give a thought to this.

#include <iostream>
#include <iomanip>

cout << setfill('0');
cout << days[xDate.DayOfWeek() -1] << AnsiString(" ")
     << setw(2) << xMonth << "/" << setw(2) << xDay
     << " " << setw(2) << xHour << ":" << setw(2)
     << xMin << " " << ampmStr <<endl;
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 1

Accepted Solution

by:
wolfpackinc earned 200 total points
ID: 6916863
These are examples of using a FormatString() function

TDateTime xDate = Now();  // xSaved.DateTimeString();
AnsiString xText = xDate.FormatString("ddd c");
 will display: Wed 4/3/02 5:10:03 PM

AnsiString xText = xDate.FormatString("ddd mm/dd h:m AM/PM");
will display:
Wed 04/03 5:15 PM

AnsiString xText = xDate.FormatString("ddd m/d h:m AM/PM");
will display:
Wed 4/3 5:20 PM

AnsiString xText = xDate.FormatString("dddd m/d h:m AM/PM");
will display:
Wednesday 4/3 5:21 PM

m     Displays the month as a number without a leading zero (1-12). If the m specifier immediately follows an h or hh specifier, the minute rather than the month is displayed.
mm     Displays the month as a number with a leading zero (01-12). If the mm specifier immediately follows an h or hh specifier, the minute rather than the month is displayed.

mmm     Displays the month as an abbreviation (Jan-Dec) using the strings given by the ShortMonthNames global variable.
mmmm     Displays the month as a full name (January-December) using the strings given by the LongMonthNames global variable.


h     Displays the hour without a leading zero (0-23).
hh     Displays the hour with a leading zero (00-23).


am/pm     Uses the 12-hour clock for the preceding h or hh specifier, and displays 'am' for any hour before noon, and 'pm' for any hour after noon. The am/pm specifier can use lower, upper, or mixed case, and the result is displayed accordingly.

/     Displays the date separator character given by the DateSeparator global variable.

:     Displays the time separator character given by the TimeSeparator global variable.






0
 
LVL 1

Expert Comment

by:wolfpackinc
ID: 6917056
>>TDateTime xDate = xSaved;  // xSaved.DateTimeString();
 Also are you saving a DateTimeString()?

Save the time as a DateTime:
MyRegistry->WriteDateTime(const System::AnsiString Name, System::TDateTime Value);

Then Read it:
MyRegistry->ReadDateTime(const System::AnsiString Name, System::TDateTime Value);

Format it:
AnsiString xText = xDate.FormatString("ddd m/d h:m AM/PM");
Display it:
Label->Caption = XText;
0
 

Author Comment

by:LordDamein
ID: 6928749
Wow.  Got back to this part of the program and remembered I had posted here.

Thanks for all of the input.  Now I have it displaying the way it should.  :)

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

12 Experts available now in Live!

Get 1:1 Help Now