get today's date in mm/dd/yyyy format

A

Abhijit

hello everybody,

I got a prob. I see a lot of date formats when I do a search in
Google. But can any one give me some pointers / an example to extract
today's date in C++ ? (formate of date: dd/mm/yyyy)

Thanks in advance
 
J

John Harrison

Abhijit said:
hello everybody,

I got a prob. I see a lot of date formats when I do a search in
Google. But can any one give me some pointers / an example to extract
today's date in C++ ? (formate of date: dd/mm/yyyy)

Thanks in advance

Untested code

#include <time.h>
#include <stdio.h>

time_t t = time(0);
tm* lt = localtime(t);
char date[11];
sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +
1900);

john
 
S

Sharad Kala

Abhijit said:
hello everybody,

I got a prob. I see a lot of date formats when I do a search in
Google. But can any one give me some pointers / an example to extract
today's date in C++ ? (formate of date: dd/mm/yyyy)

Take a look at functions in <ctime>. Also check Boost's Date-time library.

Sharad
 
D

David Fisher

I got a prob. I see a lot of date formats when I do a search in
Google. But can any one give me some pointers / an example to extract
today's date in C++ ? (formate of date: dd/mm/yyyy)

Thanks in advance

Untested code

#include <time.h>
#include <stdio.h>

time_t t = time(0);
tm* lt = localtime(t);
char date[11];
sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +
1900);

Or ...

#include <ctime>
#include <iostream>

const int MAXLEN = 80;
char s[MAXLEN];
time_t t = time(0);
cftime(s, "%D", &t);
std::cout << s << '\n';

David Fisher
Sydney, Australia
 
J

John Harrison

David Fisher said:
I got a prob. I see a lot of date formats when I do a search in
Google. But can any one give me some pointers / an example to extract
today's date in C++ ? (formate of date: dd/mm/yyyy)

Thanks in advance

Untested code

#include <time.h>
#include <stdio.h>

time_t t = time(0);
tm* lt = localtime(t);
char date[11];
sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +
1900);

Or ...

#include <ctime>
#include <iostream>

const int MAXLEN = 80;
char s[MAXLEN];
time_t t = time(0);
cftime(s, "%D", &t);
std::cout << s << '\n';

David Fisher
Sydney, Australia

Never heard of cftime, did you mean strftime? If so then %D is part of the
C99 standard, though apparently widely supported.

john
 
D

David Fisher

John said:
#include <ctime>
#include <iostream>

const int MAXLEN = 80;
char s[MAXLEN];
time_t t = time(0);
cftime(s, "%D", &t);
std::cout << s << '\n';

Never heard of cftime, did you mean strftime? If so then %D is part of the
C99 standard, though apparently widely supported.

Hmm ... I'm not sure how standard cftime() is, actually - but it wasn't a
typo ...

Using strftime() it would be:

#include <ctime>
#include <iostream>

const int MAXLEN = 80;
char s[MAXLEN];
time_t t = time(0);
strftime(s, MAXLEN, "%m/%d/%Y", localtime(&t));
std::cout << s << '\n';

("%D" with strftime seems to only have 2 digits - ?)

David Fisher
Sydney, Australia
 
J

Jon Bell

Abhijit said:
[...] can any one give me some pointers / an example to extract
today's date in C++ ? (formate of date: dd/mm/yyyy)

sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +

I thought he was asking about how to *read* a date from an input stream.
 
J

John Harrison

Jon Bell said:
Abhijit said:
[...] can any one give me some pointers / an example to extract
today's date in C++ ? (formate of date: dd/mm/yyyy)

sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +

I thought he was asking about how to *read* a date from an input stream.

You could well be right. Assuming said date is held in a C string, then

int day, month, year;
sscanf(date, "%d/%d/%d", &day, &month, &year);

does the trick, but with minimal error checking.

john
 
M

Matt Hurd

I got a prob. I see a lot of date formats when I do a search in
Google. But can any one give me some pointers / an example to extract
today's date in C++ ? (formate of date: dd/mm/yyyy)
In Boost's (www.boost.org) date time library you might use:

date from_uk_string(const std::string&)

Where the parameter is a delimited date string where with order
day-month-year eg: 25-1-2002.

Reference: http://www.boost.org/libs/date_time/doc/class_date.html

Hope this helps,

Matt Hurd
www.hurd.com.au
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top