比如现在有字符串 "20081225"加上六个月后值。要怎么求。在线等

feng8317 2008-12-12 07:06:45
比如现在有字符串 "20081225"加上六个月后值。要怎么求。在线等
...全文
45 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingcsx666 2008-12-12
  • 打赏
  • 举报
回复
yes,先转化为日期类型,在加六个月就可以了
lvfeng180 2008-12-12
  • 打赏
  • 举报
回复
顶,我看行
烈火蜓蜻 2008-12-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Linux7985 的回复:]
很明显,先转换成日期类型,再转回字符串,是比较简单的

C# code
string Datestr = DateTime.ParseExact( "20081225", "yyyyMMdd", null ).ToString( "yyyyMMdd" );
[/Quote]

少加了6个月

string Datestr = DateTime.ParseExact( "20081225", "yyyyMMdd", null ).AddMonths( 6 ).ToString( "yyyyMMdd" );

GTX280 2008-12-12
  • 打赏
  • 举报
回复

string str = "20081225";
DateTime time1 = DateTime.ParseExact(str, "yyyyMMdd", null);//先转化成DateTime类型的
str = time1.AddMonths(6).ToString("yyyyMMdd");//加上6个月并转化成与str一致的字符串
wesleyluo 2008-12-12
  • 打赏
  • 举报
回复
转换成日期后,直接相加.
DateTime.Add()
DateTime.AddHours();
DateTime.AddDays();
....
烈火蜓蜻 2008-12-12
  • 打赏
  • 举报
回复
很明显,先转换成日期类型,再转回字符串,是比较简单的

string Datestr = DateTime.ParseExact( "20081225", "yyyyMMdd", null ).ToString( "yyyyMMdd" );
lchh0917 2008-12-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hulihui 的回复:]
C# code string s = "20081225";
int y = int.Parse(s.Substring(0, 4));
int m = int.Parse(s.Substring(4, 2));
int d = int.Parse(s.Substring(6, 2));
DateTime dt0 = new DateTime(y, m, d);
DateTime dt1 = dt.AddMonths(6);
MessageBox.Show(dt1.ToString());
[/Quote]
s.Substring(a,b)中参数a有问题吧
长沙三毛 2008-12-12
  • 打赏
  • 举报
回复
            string s = "20081225";
int y = int.Parse(s.Substring(0, 4));
int m = int.Parse(s.Substring(4, 2));
int d = int.Parse(s.Substring(6, 2));
DateTime dt0 = new DateTime(y, m, d);
DateTime dt1 = dt.AddMonths(6);
MessageBox.Show(dt1.ToString());

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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