怎么把日期转换成字符串类型?

cocat 2010-06-30 09:49:09

class DateType{
public:
DateType(int year = 0, int month = 0, int day = 0);
~DateType();
void DateToString();
private:
int y;
int m;
int d;
};
void DateType::DateToString()
{
////////////////////////
}


怎么转换啊?有点不解
...全文
537 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2010-07-01
  • 打赏
  • 举报
回复
COleDateTime::Format
Csuxiaowu 2010-07-01
  • 打赏
  • 举报
回复
12楼的 我喜欢
xt0601 2010-06-30
  • 打赏
  • 举报
回复
关注ING……
doublemoon 2010-06-30
  • 打赏
  • 举报
回复
void DataToString(int year,int month,int days)
{
char a_year[5],a_month[3],a_day[3];

itoa(year,a_year,10);
itoa(month,a_month,10);
itoa(days,a_day,10);

char strings[9];
strcpy(strings,a_year);
strcat(strings,a_month);
strcat(strings,a_day);

printf("the string is: %s\n",strings);
}
djjlove_2008 2010-06-30
  • 打赏
  • 举报
回复
这么复杂干什么了?
istringstream(stream);
while(stream >> str)
cout << str << endl;
cocat 2010-06-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wyz007134 的回复:]
C/C++ code
#include <iostream>
#include <string>
using namespace std;

class DateType{
public:
DateType(int year = 0, int month = 0, int day = 0);
~DateType();
const string DateTo……
[/Quote]

这个挺好~呵呵
走好每一步 2010-06-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 cocat 的回复:]
哎~把1988 10 23 变成"1988 10 23"得那么复杂?有没啥函数啊?
[/Quote]

sprintf(buff, "%d年", 1977);
sprintf(buff2, "%d月", 3);
strcat(buff, buff2);
benbshmily 2010-06-30
  • 打赏
  • 举报
回复
char ch[64]= {0};
sprintf(ch,"%d %d %d", y, m, d);
string str = new string(ch);
return str;
???
wyz007134 2010-06-30
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

class DateType{
public:
DateType(int year = 0, int month = 0, int day = 0);
~DateType();
const string DateToString();
private:
int y;
int m;
int d;
};
//Constructor
DateType::DateType(int year, int month, int day) : y(year), m(month), d(day)
{ cout << "Constructor called\n"; } //关于构造数据的合法性,烦请lz自己补上

DateType::~DateType(){ cout << "Destructor called\n";}

const string DateType::DateToString()
{
//sizeof("2010-06-30") = 11;
char date[11];
date[4] = '-';
date[7] = '-';
date[0] = y / 1000 + 48;
date[1] = y / 100 % 10 + 48;
date[2] = y / 10 % 10 + 48;
date[3] = y % 10 + 48;
date[5] = m / 10 + 48;
date[6] = m % 10 + 48;
date[8] = d / 10 + 48;
date[9] = d % 10 + 48;
date[10] = '\0';
const string str(date);
return str;
}

int main()
{
DateType date1(1995, 12, 9), date2(2010, 6, 30);
cout << "date1 : " << date1.DateToString() << endl;
cout << "date2 : " << date2.DateToString() << endl;
return 0;
}
liutengfeigo 2010-06-30
  • 打赏
  • 举报
回复
没有。。。。
cocat 2010-06-30
  • 打赏
  • 举报
回复
哎~把1988 10 23 变成"1988 10 23"得那么复杂?有没啥函数啊?
liutengfeigo 2010-06-30
  • 打赏
  • 举报
回复
liutengfeigo 2010-06-30
  • 打赏
  • 举报
回复
#include <sstream>
liutengfeigo 2010-06-30
  • 打赏
  • 举报
回复

istringstream stream;
string result=”123456”;
int n=0;
stream << result; //从字符串输入
stream >> n; //输出到int
n 就等于123456了

int 到string 是一样的道理
cocat 2010-06-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 thefirstz 的回复:]
stringstream 之类的都可以~~
[/Quote]
没看懂。。
昵称很不好取 2010-06-30
  • 打赏
  • 举报
回复
stringstream 之类的都可以~~
liutengfeigo 2010-06-30
  • 打赏
  • 举报
回复

//输入一个整数,递归转化成字符串输出 输入123 输出"123".
#include <iostream>
using namespace std;
int n = 0;
void change(int num, char str[])
{
if (num / 10 != 0)
change(num / 10, str);
num %= 10;
str[n] = num + '0';
n++;
str[n] = '\0';
}

int main()
{
int num;
char str[81];
cin >> num;
change(num, str);
cout << str << endl;
system("pause");
return 0;
}

mstlq 2010-06-30
  • 打赏
  • 举报
回复
void DateType::DateToString()

const string DateType::DateToString()
吧……

接口很不好

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧