?
Solved

Displaying the Time in a label

Posted on 2002-04-02
6
Medium Priority
?
817 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

762 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