iT邦幫忙

0

c#依建檔日期每三個月寄送提示通知

web
  • 分享至 

  • twitterImage

請問各位大大
如何撰寫c#依建資料日期每三個月寄送提示通知?
萬一是剛好 31 日建立 三個月後卻遇到了沒有31日的月份或是二月份
該如何處理比較好啊! 暈

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

8
ycl8000
iT邦高手 1 級 ‧ 2012-09-21 13:53:24
最佳解答

自己試看看結果.

<pre class="c" name="code">
        static void Main(string[] args)
        {
            DateTime t1 = DateTime.Parse("2012/11/30");
            DateTime t2 = t1.AddMonths(3);  //加3個月
            Console.WriteLine("{0}", t2);   // 2013/2/28
            Console.ReadKey();
        }
雷夢卡爾 iT邦研究生 5 級 ‧ 2012-09-21 15:09:55 檢舉

稍微修改上面的CODE

<pre class="c" name="code">
        static void Main(string[] args)
        {
            DateTime t1 = DateTime.Parse("2012/11/30");
            for (int i = 1; i <= 240; i++)
            {
                int addMonths = 3 * i;
                //判斷建資料日期是否大於每隔三個月的那個月裏的天數,若是則以當月最後一天為主、否則已建立的日期為主。
                if (t1.Day > DateTime.DaysInMonth(t1.AddMonths(addMonths).Year, t1.AddMonths(addMonths).Month))
                {
                    Console.WriteLine(t1.AddMonths(addMonths).Year + "/" + t1.AddMonths(addMonths).Month + "/" + DateTime.DaysInMonth(t1.AddMonths(addMonths).Year, t1.AddMonths(addMonths).Month));
                }
                else
                {
                    Console.WriteLine(t1.AddMonths(addMonths).Year + "/" + t1.AddMonths(addMonths).Month + "/" + t1.AddMonths(addMonths).Day);
                }
            }
            Console.ReadKey();
        }
4
huangsb
iT邦好手 1 級 ‧ 2012-09-22 01:25:16

如果需求是每3個月,ycl8000 的範例就是正確的,而且簡單。

如果是指定3個月後的某一天,則 lemoncar 算是正確的,但不夠清楚。

我要發表回答

立即登入回答