取得每個月的最後一天,以下是實驗用到的代碼

來源:互聯網
上載者:User

現在用到要取得月中的最後一天,在 sql 中用 2007-02-30 這個日期試了一下,出錯了,所以必須用有效日期,
以下的代碼是實驗用到的

 

procedure TForm1.Button1Click(Sender: TObject);
var
  fisc:string;
  ss : string;
  m1,m2:Tdatetime;
  nextMonth:tdatetime;
  curMonth:tdatetime;
  maxDay:string;
  m,y:dword;
  ms,ys:string;

begin

  m1 := strtodate('2007-09-29');
  m2 := strtodate('2007-10-28');

  m :=dateutils.MonthOf(m2);
  y := dateutils.YearOf(m2);

  ms :=inttostr(m);
  ys :=inttostr(y);

  nextMonth := strtodate(ys+'-'+inttostr(m+1)+'-01');  // 取得下一個月的 1 號
  curMonth :=  strtodate(ys+'-'+inttostr(m)+'-01');    // 取得當月的 1 號

 showmessage(floattostr(nextMonth-curMonth));                // 31
 showmessage(datetostr( dateutils.EndOfTheMonth(curMonth))); // 2007-10-31
 showmessage(datetostr( dateutils.EndOfTheDay(curMonth)));   // 2007-10-01
 showmessage(datetostr( dateutils.EndOfTheYear(curMonth)));  // 2007-12-31
 showmessage(datetostr( dateutils.EndOfAMonth(y,m)));        // 2007-10-31
end; 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.