Solved

Displaying the Time in a label

Posted on 2002-04-02
6
811 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

813 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

16 Experts available now in Live!

Get 1:1 Help Now