Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Displaying the Time in a label

Posted on 2002-04-02
6
Medium Priority
?
818 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Accepted Solution

by:
wolfpackinc earned 800 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

610 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