获取n个月的日期C# [英] Get date for n months C#

查看:67
本文介绍了获取n个月的日期C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人如何获得下个月的日期为10个月或任何月份我尝试但只能让我6个月



什么是正确的句法



我的尝试:



hi every body how to get the dates of next month for 10 months or any number of months i try that but only gets me 6 months

what is the correct syntactics

What I have tried:

var x = 10; // 10months
          for (int i = 0; i < x; i++)
          {
              DateTime sixmonthsfromnow = today.AddMonths(x);
              MessageBox.Show(sixmonthsfromnow.ToShortDateString());
              x--;
          }

推荐答案

为什么要在for循环中更改x的值?你可以在for循环中做的最糟糕的事情是改变循环中的迭代器(i)或常量(x)的值。



是你真的想要这样做吗?



Why are you changing the value of x inside the for loop? The worst thing you could possibly do in a for loop is to change the value of either the iterator (i) or the constant (x) INSIDE the loop.

Are you actually wanting to do this?

StringBuilder str = new StringBuilder();
var x = 10; // 10months
for (int i = 0; i < x; i++)
{
    DateTime sixmonthsfromnow = DateTime.Now.AddMonths(i+6);
    str.AppendLine(sixmonthsfromnow.ToString("yyyy MM dd"));
}
MessageBox.Show(str.ToString());





编辑====== =======



如果您认为我的解决方案非常糟糕,那么如果您将自己的解决方案投票给自己的解决方案。 FFS。



EDIT =============

Whoever voted this a 1 - post your own solution if you think mine is so bad. FFS.


感谢John Simmons / outlaw这是我需要的





thanks for John Simmons / outlaw That's i need


StringBuilder str = new StringBuilder();
          var x = 10; // 10 months total period
          var y = 1; // variable for increase month every one,two ... month
          for (int i = 0; i < x; i++)
          {
              DateTime sixmonthsfromnow = DateTime.Now.AddMonths(i + y);
              str.AppendLine(sixmonthsfromnow.ToString("yyyy/MM/dd"));
          }
          MessageBox.Show(str.ToString());


这篇关于获取n个月的日期C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆