gandhigcpp
gandhigcpp

Reputation: 587

Reading the month part in the date in c++

I have two dates namely the filecreation date and the system date both in the int format as

        "20120304","20120404" which is something like YYYYMMDD.

i just want to take the MM part of these two dates these two dates are returned from a function as integers .

Please suggest some examples

Thanks

Upvotes: 0

Views: 262

Answers (3)

Vijay
Vijay

Reputation: 67221

although i prefer @keety's answer,there is an other way. u can use string streams:

for eg:

#include <sstream>

int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

so after you have your string as YYYYMMDD in a string varaible. substr(4,2) will be your month. you can later on use strtol for converting back to integer.

Upvotes: 0

keety
keety

Reputation: 17441

since it is int type use int mm=(yyyymmdd/100)%100

Upvotes: 3

wallyk
wallyk

Reputation: 57774

If they are always in the format YYYYMMDD, then take the substring at [4] for length 2.

Upvotes: 0

Related Questions