473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Extracting data from a date

I have a program that gets a date from the user in the format dd/mm/yy, and
I need to extract the d's, m's, and y's to be used within the program. I
have the program get the date as a string. How can I get the days, month,
and year numbers out of it?
Sep 25 '05 #1
2 2869
deanfamily11 wrote:
I have a program that gets a date from the user in the format dd/mm/yy, and
I need to extract the d's, m's, and y's to be used within the program. I
have the program get the date as a string. How can I get the days, month,
and year numbers out of it?


Use a stringstream, which lets you read from a string just like any
other stream.

#include <sstream>

std::istringstream buffer(date);
int day, mth, yr;
char slash1, slash2;
buffer >> day >> slash1 >> mth >> slash2 >> yr;

No error checking here of course, but that would probably be easier to
do on string than the stringstream.

john
Sep 25 '05 #2

deanfamily11 wrote:
I have a program that gets a date from the user in the format dd/mm/yy, and
I need to extract the d's, m's, and y's to be used within the program. I
have the program get the date as a string. How can I get the days, month,
and year numbers out of it?


You may use sscanf() for string parsing.

#include <stdio.h>

struct date
{
unsigned short year, month, day;
};

int str_to_date(char const* s, date* d)
{
return 3 == sscanf(s, "%2hu/%2hu/%4hu", &d->day, &d->month,
&d->year)
? 0
: -1
;
}

int main(int ac, char** av)
{
date d;
if(str_to_date(av[1], &d))
printf("bad date format\n");
else
printf("date: %02hu/%02hu/%04hu\n", d.day, d.month, d.year);
}

Sep 26 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: v0lcan0 | last post by:
Any help on extracting the time part from the datetime field in SQL database. even though i had entered only the time part in the database when i extract the field it gives me only the date...
5
by: klall | last post by:
Hello. I need to extract date information from a memo field entered in the following way: 01/01/2005 - 31/12/2005 01/01/2004 - 31/12/2004 01/01/2003 - 31/12/2003 01/01/1996 - 31/12/1996. The...
2
seshu
by: seshu | last post by:
hi everybody i have a greate problem in taking the time in date time what iam doning here is if i want time iam using time(column name) and for date i am using date(column name)...
1
by: jerryyang_la1 | last post by:
I'm reading in a CSV and displaying using the code below: $users = file("text.txt"); echo "<table border='1' width='75%' align='center'>"; echo "<tr>"; echo "<td width='33%'...
2
by: robinsiebler | last post by:
I had nothing better to do, so I thought I would make a database that contained the songs played on the internet radio station I listen to (hardradio.com) so I could see how many differents...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
4
by: dexter48 | last post by:
Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string...
4
by: poolboi | last post by:
hi guys i've having some problem extracting data from a text file example if i got a text file with infos like: Date 2008-05-01 Time 22-10 Date 2008-05-01 Time 21-00 Date 2008-05-02 Time...
10
by: Mikesinfo | last post by:
Hi everyone, Hopefully this is the right forum to use. I am programming in C# and I've been racking my brain and searching for the right answer to extract my information according to after a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.