1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

日付型のフォーマットをYYYY/MM/DD→YYYYMMDDに変換する際、数式とApexの記載方法を別々説明

Last updated at Posted at 2023-05-30

数式の記載方法:
日付型のフォーマットをYYYY/MM/DD→YYYYMMDDに変換する際
LPADという関数を使います。
LPADは、指定した桁数になるまで文字列の左側に文字列を埋め込む関数である。
参照資料
https://help.salesforce.com/s/articleView?id=sf.customize_functions_lpad.htm&type=5
下記の数式を記載します。
{!TEXT(YEAR(日付API名)) &
LPAD(TEXT(MONTH(日付API名)), 2 , '0') &
LPAD(TEXT(DAY(日付API名)), 2 , '0')}

出力結果:YYYYMMDD

Apexの記載方法:

apex
Date date1 = 日付API名;
String dateMonth = String.valueof(date1.month());
String dateDay = String.valueof(date1.day());
if(dateMonth.length()==1){
   dateMonth = '0' + dateMonth;
}
if(dateDay.length()==1){
   dateDay = '0' + dateDay;
}
String iDate = String.valueof(date1.year()) + dateMonth + dateDay ;

出力結果:YYYYMMDD

追記:

対象年月日の日付を1日にしたものを開始日とする。
開始日に1か月を加算したものを終了日とする。
場合の計算方法:

apex
Date objectDate=Date.newInstance( 2014, 08, 24);
Integer yearNum = Integer.valueof(objectDate.year());
Integer monthNum = Integer.valueof(objectDate.month());
Date startDay = Date.newInstance( yearNum, monthNum, 01);
Date finishDay = startDay.addMonths(1);
system.debug(startDay);//2014-08-01 00:00:00
system.debug(finishDay);//2014-09-01 00:00:00
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?