Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-20-2004, 05:05 PM   #1 (permalink)
ten_d
Registered User
 
Join Date: Mar 2004
Posts: 2
ten_d is on a distinguished road
Trying to get mm/dd/yy format

Hi all,

I'm a total mega C++ newb.. I'm trying to figure out how to get a normal date that a user input from mm/dd/yyyy format to mm/dd/yy format.

Please help!! Thanks!
ten_d is offline   Reply With Quote
Old 03-21-2004, 06:45 AM   #2 (permalink)
ten_d
Registered User
 
Join Date: Mar 2004
Posts: 2
ten_d is on a distinguished road
This may help a bit. Here's my class so far.

//Date Class definition
#ifndef DATE1_H
#define DATE1_H
//#include <iostream>

class Date {

public:
Date( int = 1, int = 1, int = 1990); //defult constructor
void print() const; //print date in month/day/year format
void printJulian() const; //print in julian format ddd/yyyy
~Date();

private:
int month;
int day;
int year;

int checkDay(int) const;

}; //end of class Date
#endif

Date::Date(int mn, int dy, int yr)
{
cout << "Enter the month: ";
cin >> mn;
cout << "Enter the day: ";
cin >> dy;
cout << "Enter the year: ";
cin >> yr;

if (mn > 0 && mn <= 12)
month = mn;

else {
month = 1;
cout << "Month " << mn << " is invalid. Month is set to 1.\n";
}

year = yr;
day = checkDay(dy);

} //end Date constructor



//print Date object in form month/day/year
void Date::print() const
{
cout << "The day is " << month << '/' << day << '/' << year << endl;
} //end of function print

//output Date object to show when its destructor is called
Date::~Date()
{

} //end destructor ~Date



//utility function to confirm proper day value based on month and year, handles leap years too
int Date::checkDay(int testDay) const
{
static const int daysPerMonth[13] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//determine whether testDay is valid for specified month
if (testDay > 0 && testDay <= daysPerMonth [month])
return testDay;

//Feb 29 check
if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
return testDay;

cout << "Day " << testDay << " is invalid. Date set to 1.\n";

return 1;
} //end of function checkDay



void Date::printJulian() const
{
static const int daysPerMonth[13] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int julDate = day;

for (int counter =0; counter < month; counter++)
{
julDate = julDate + daysPerMonth[counter];
}

if ((year % 400 == 0 || (year % 4 ==0 && year % 100 != 0)) && (month >= 2 && day > 28))
julDate++;

cout << "The date in Julian style ddd/yyyy is " << julDate << '/' << year << endl;
} //end of printJulian function
ten_d is offline   Reply With Quote
Old 03-21-2004, 11:01 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Date and time fuctions in MS Windows is one of the less trivial things I'm affraid.
But here is a nice approach:
Code:
#include <ctime>
#include <iostream>

using namespace std;

time_t timeDate;
struct tm *currDate;

int main()
{
	char yearBuffer[4];

	timeDate = time(0);
	currDate = localtime(&timeDate); //DOS systemcall!
		
	int month = currDate->tm_mon+1;
	int day = currDate->tm_mday;
        //int year = currDate->tm_year+1900; //Outputs: 2004 
	strftime( yearBuffer, 4, "%y", currDate ); //Output: 04 (instead of 2004)

	cout<<day<<" "<<month<<" "<<yearBuffer<<endl;

	return 0;
}
Just a little hint for the upcoming programmer FWIW:
The first thing is clarity. Check out this:
Quote:
Date set to 1.
What is this?
Val: 'What is the date today?'
Alex: 'Today is 1'.
Val: 'Huh? What was the date of yesterday or the day before yesterday then?'
Alex: '...'

Do you see what I mean?
GL HF!
__________________
Valmont is offline   Reply With Quote
Old 03-21-2004, 09:04 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Otherwise this:

If you don't have the time to check out the date libraries in C++ then here is your code revised.

- You definatly want to check out printShortYear():
It answers your question more directly. Look at the "itoa()" and the "string::assign()" function. Both of them come with the standard C++ library.

- I've taken the liberty to remove the comments:
It is obvious what these methods do, so lets not add them.
Certainly don't add comments for constructors. People should (and will) recognize constructors/destructors on-the-fly.


- I've removed the default values in your default ctor:
Default values in a default ctor takes away flexibility. Instead, I've added a ctor wich takes three arguments.

- I've broken certain algorithms in smaller parts.
This way there is no double code. It also makes the various methods more readable. Clearness is all.

Here is the complete code, a main function included to demonstrate it's workings:
In CDate.h
Code:
#ifndef CDATE_H
#define CDATE_H

#include <iostream>
#include <string>

using namespace std;

static const int daysPerMonth[13] = 
	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

class Date 
{
public:
	Date() { GetDate(); ValidateDate(); } 
	Date(int mt, int dy, int yr) : month(mt), day(dy), year(yr) {	ValidateDate(); }
	~Date() { };
public:
	void print() const; 
	void printJulian() const;
	
private:
	int month;
	int day;
	int year;
	void GetDate();
	void ValidateDate();
	bool CheckValidDay();
	bool CheckForLeapYear() const;
	string printShortYear() const;
	void reset_istream(istream & is);
}; 

void Date::GetDate()
{	
retry_month: cout << "Enter the month: ";
	cin >> month; 
	if(cin.fail()) { reset_istream(cin); goto retry_month; return; }
retry_day: cout << "Enter the day: ";
	cin >> day;
	if(cin.fail()) { reset_istream(cin); goto retry_day; return; }
retry_year: cout << "Enter the year: ";
	cin >> year;	
	if(cin.fail()) { reset_istream(cin); goto retry_year; return; }
}

void Date::ValidateDate()
{
	if ((month > 0 && month <= 12)==false)
		{
			month = 1;
			cout << "Month is invalid. Month has been re-set to 1.\n";
		}	
	//Leapyear and valid day in current month.
	if(!CheckValidDay())
		{ cout<<"Day is invalid, day has been re-set to 1"<<endl; day =1; }
}

void Date::print() const
{	
	cout << "The date is " << month << '/' << day <<
		'/'<<printShortYear()<<endl; 
} 

void Date::printJulian() const
{	
	int julDate = day;
	for (int counter =0; counter < month; counter++)
		julDate = julDate + daysPerMonth[counter];	
	if ((year % 400 == 0 ||CheckForLeapYear() ) && (month >= 2 && day > 28))
		julDate++;

	cout <<"The date in Julian style ddd/yy is " << julDate << 
		'/'<<printShortYear()<< endl; 
} 

string Date::printShortYear() const
{
	char szYearBuffer[4];
	string sShortYear;
	itoa(year, szYearBuffer, 10);
	sShortYear.assign(szYearBuffer, 2, 2);
	return sShortYear;
}

bool Date::CheckForLeapYear() const
{	return (year % 4 ==0 && year % 100 != 0); }

bool Date::CheckValidDay() 
{
	if (!(day > 0 && day <= daysPerMonth [month]))
		return (month == 2 && day == 29 && CheckForLeapYear());	
	
	return true;
} 

void Date::reset_istream(istream & is)
{
	char ch;
	// Reset the state. 
	is.clear();
	// Remove all characters until we find a newline or EOF.
	ch = is.get();
	while ((ch != '\n')&&(ch != EOF))
		ch = is.get();	
	is.clear();
}

#endif //CDATE_H
Just ask if you need more help.
__________________
Valmont is offline   Reply With Quote
Old 03-22-2004, 07:54 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Oops, I forgot the main() function:
Code:
#include "CDate.h"

int main()
{
	Date MyDate;
	MyDate.print();
	MyDate.printJulian();
	
	cout<<endl;

	Date MyOtherDate(2, 29, 2004);
	MyOtherDate.print();
	MyOtherDate.printJulian();
	
	return 0;
}
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
using sprintf to format a number Epsilon PHP 1 08-24-2004 07:30 PM
email address format trevor PHP 8 12-23-2003 05:25 AM
windows timestamp format? sde Windows 4 07-18-2003 12:09 PM
Movies CaN Opener HTML, XML, Javascript, AJAX 5 03-08-2003 09:37 AM
format numbers sde PHP 3 10-03-2002 11:10 AM


All times are GMT -8. The time now is 11:20 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting