Solved

Displaying the Time in a label

Posted on 2002-04-02
6
813 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
[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
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
Industry Leaders: 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

730 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