日付のフォーマットを変える

# フローの場合

+ [【Flow】「日付/時間」項目の値を「日付」項目に反映されない](https://trailhead.salesforce.com/trailblazer-community/feed/0D5KX000000USai)

:::note
数式リソースを使って変換してみて下さい 

{!$System.OriginDateTime}は日時型の項目に置き換えてください 
 
```
DATEVALUE({!$System.OriginDateTime})
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/9e7801b4-f709-4be9-8eb6-427461ec77f3.png)
:::


# yyyyMMdd

+ [項目を記入すると2024/07/24となるが、自動入力時には20240724という形にしたい](https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007eqNZGSA2)

```
TEXT(YEAR(項目)) +
LPAD(TEXT(MONTH(項目)), 2, "0")  +
LPAD(TEXT(DAY(項目)), 2, "0") +  
```


# d/M/yyyy format

+ [How to convert text to date in Flow](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54V00007XIUT7)

```
DATEVALUE(
RIGHT({!$Record.Datefield__c},4) & 
"-" &
IF((MID({!$Record.Datefield__c},3,1) ="/",
IF((MID({!$Record.Datefield__c},6,1) ="/",RIGHT({!$Record.Datefield__c},2)& "-"  & MID({!$Record.Datefield__c}4,2),RIGHT({!$Record.Datefield__c},2) & "-0" & MID({!$Record.Datefield__c}4,1)),
IF((MID({!$Record.Datefield__c},5,1) ="/","0" & RIGHT({!$Record.Datefield__c},1))& "-" &MID({!$Record.Datefield__c}3,2),"0" & RIGHT({!$Record.Datefield__c},1))& "0-" &MID({!$Record.Datefield__c}3,1))
)
)
```

# DateTime型から日付をテキストで抜き取る

```
 left(TEXT(CreatedDate),FIND(' ', TEXT(CreatedDate))-1)
```

#  ISO 8601 date format

[How do I format Created Date in the ISO 8601 date format?](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000DezaRSAR)

```
TEXT(YEAR(DATEVALUE( CreatedDate ))) + "-" +
LPAD(TEXT(MONTH(DATEVALUE(CreatedDate))), 2, "0") + "-" +
LPAD(TEXT(DAY(DATEVALUE(CreatedDate))), 2, "0") +  
"T" +
LPAD(TEXT(HOUR(TIMEVALUE(CreatedDate))), 2, "0") + ":" +
LPAD(TEXT(MINUTE(TIMEVALUE(CreatedDate))), 2, "0") + ":" +
LPAD(TEXT(SECOND(TIMEVALUE(CreatedDate))), 3, "0") + "Z"
```

# Date型、Time型の２つのフィールドから DateTime型のフィールドを作る

```
TEXT( Start_Date_Cstm_Date__c ) + " " + LEFT(TEXT( Start_Time__c ),8)
```

# Wednesday, 29 September 2021 の表示に変える
WEEKDAY関数を使った方がよさそうです。

```
CASE(WEEKDAY(Date), 1, "Sunday",  2, "Monday",   3, "Tuesday",   4, "Wednesday",   5, "Thursday",   6, "Friday",    "Saturday" )
+ "," +
TEXT(DAY( Date ))
+ " " +
CASE(   MONTH( date ),   1, "January",   2, "February",   3, "March",   4, "April",   5, "May",   6, "June",   7, "July",   8, "August",   9, "September",   10, "October",   11, "November",   "December" )  
+ " " +
TEXT(YEAR( Date ))
```

[Display date field in the format ddd dd-mmm-yyyy Eg: Tue, 20 Sep 2022](https://trailhead.salesforce.com/en/trailblazer-community/feed/0D54S00000Jywwc)

```
CASE(   MOD( date - DATE( 1900, 1, 7 ), 7 ),   0, "Sunday",   1, "Monday",   2, "Tuesday",   3, "Wednesday",   4, "Thursday",   5, "Friday",    "Saturday" )
+ "," +
TEXT(DAY( Date ))
+ " " +
CASE(   MONTH( date ),   1, "January",   2, "February",   3, "March",   4, "April",   5, "May",   6, "June",   7, "July",   8, "August",   9, "September",   10, "October",   11, "November",   "December" )  
+ " " +
TEXT(YEAR( Date ))
```

[Formula field to show day of week and date in DDMMYYYY](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000BsRQySAN)


# Date/Time を Data型の MMDDYYYY フォーマットに変更する

```
LPAD(TEXT(MONTH(DATEVALUE([SVMXC__Service_Order__c].Scheduled_Date_End_Time__c))), 2, "0") + 
LPAD(TEXT(DAY(DATEVALUE([SVMXC__Service_Order__c].Scheduled_Date_End_Time__c))), 2, "0") + 
TEXT(YEAR(DATEVALUE([SVMXC__Service_Order__c].Scheduled_Date_End_Time__c)))
```

[Hello, need some help converting a Date/Time field to Date, but with MMDDYYYY format.](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000DbzJiSAJ)

# 時間を24時間形式から12時間形式に変換する

+ こっちが正しい

["Created By" field extracting time and AM/PM](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000FSfGK)
[Pull DateTime field into text field](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000FWpTYSA1)

ただし、GMTになっているので調整は必要

```
IF( 
       Value(LEFT(RIGHT(text( CreatedDate ),9),2)) < 12, 
       LEFT(RIGHT(text( CreatedDate ),9),2) + ":" + LEFT(RIGHT(text( CreatedDate ),6),2) + " AM",
 
       IF( 
              Value(LEFT(RIGHT(text( CreatedDate ),9),2)) = 12,
              LEFT(RIGHT(text( CreatedDate ),6),2) + ":" + LEFT(RIGHT(text( CreatedDate ),6),2) + " PM",
              TEXT(Value(LEFT(RIGHT(text( CreatedDate ),9),2)) - 12)) + ":" + LEFT(RIGHT(text( CreatedDate ),6),2) + " PM" 

   )
```



+ これは機能しない
[時間を24時間形式から12時間形式に変換する](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000BuPBU)

よくわからん。 はずだ、これは単に HH:MMにしかならん。騙された。

```
LEFT(RIGHT(text( CreatedDate ),9),8)
```
# 時間を12時間形式から24時間形式に変換する

GMTになっている。

[formula on report to get the 24h format](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000EAHg0SAH)

#Auraのjs で時刻の変換。

[Convert '12:15:00.000' to 12:15 PM](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000DzmU5SAJ)

+ [How can I change this formula to give me as a result a text with the Date and the hour with 24 hours format?](https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007cmQuL)



# 経過時間の表示

日にち

```
IF(FLOOR( (NOW()- CreatedDate)) > 0,TEXT( FLOOR( (NOW()- CreatedDate)) ) & " days ", "")
```

日時、秒

```
IF(FLOOR( (NOW()- CreatedDate)) > 0,TEXT( FLOOR( (NOW()- CreatedDate)) ) & " days ", "") 
& IF(FLOOR( MOD( (NOW()- CreatedDate)* 24, 24 ) )>0,TEXT( FLOOR( MOD( (NOW()- CreatedDate)* 24, 24 ) ) ) & " hours ","") 
& TEXT( ROUND( MOD( (NOW()- CreatedDate)* 24 * 60, 60 ), 0 ) ) & " minutes "
& TEXT( ROUND( MOD( (NOW()- CreatedDate)* 24 * 60*60, 60 ), 0 ) ) & " seconds"
```

https://salesforce.stackexchange.com/questions/177219/formula-fields-date-and-date-time-case-elapsed-duration

# 選択リストの数値でない値を使って変換する　generate 4/1/2022.

```
IF(ISBLANK(TEXT(MonthsPicklist__c)), NULL,
DATE(
YEAR(TODAY()),
CASE(MonthsPicklist__c,
"January",1, 
"February",2, 
"March",3, 
"April",4,
"May",5,
"June",6, 
"July",7, 
"August",8, 
"September",9,
"October",10,
"November",11,12
), 1))
```

[Formula to turn a picklist selection into a date](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000FxIjxSAF)

# 2021-01-18T05:00:00.000Z形式の文字列を日時型に変換する

```
DATETIMEVALUE(
SUBSTITUTE(
SUBSTITUTE( zulu_time__c ,'T',' '),'.000Z','')
)
```

# 和暦

[[数式 サンプル] 日付型項目の値を和暦表示する数式](https://help.salesforce.com/s/articleView?id=000320752&type=1)

```
IF ( AND ( DATE__C >= DATE (1868,09,08), DATE__C <= DATE (1912,07,29)), "明治" & TEXT ( YEAR ( DATE__C ) - 1867 ) & "年" & TEXT ( MONTH ( DATE__C )) & "月" & TEXT ( DAY ( DATE__C )) & "日", 
IF ( AND ( DATE__C >= DATE (1912,07,30), DATE__C <= DATE (1926,12,24)), "大正" & TEXT ( YEAR ( DATE__C ) - 1911) & "年" & TEXT ( MONTH ( DATE__C )) & "月" & TEXT ( DAY ( DATE__C )) & "日", 
IF ( AND ( DATE__C >= DATE (1926,12,25), DATE__C <= DATE (1989,01,07)), "昭和" & TEXT ( YEAR ( DATE__C ) - 1925 ) & "年" & TEXT ( MONTH ( DATE__C )) & "月" & TEXT ( DAY ( DATE__C )) & "日", 
IF ( AND ( DATE__C >= DATE (1989,01,08), DATE__C <= DATE (2019,04,30)), "平成" & TEXT ( YEAR ( DATE__C ) - 1988 ) & "年" & TEXT ( MONTH ( DATE__C )) & "月" & TEXT ( DAY ( DATE__C )) & "日", 
IF ( DATE__C >= DATE (2019,05,01), "令和" & TEXT ( YEAR ( DATE__C) - 2018) & "年" & TEXT ( MONTH ( DATE__C )) & "月" & TEXT ( DAY ( DATE__C )) & "日", 
"#エラー")))))
```


# Apex YYYY-MM-DDT00:00:00Zに変更する

```
String dateFormat = 'yyyy-MM-dd\'T\'HH:mm:ss\'Z\'';

DateTime dt = DateTime.now();
String dateString = dt.format(dateFormat);
System.debug(dateString);
```

[I want to change the Date Time format of below line to YYYY-MM-DDT00:00:00Z](https://trailhead.salesforce.com/en/trailblazer-community/feed/0D54S00000JzwvQ)

# 6-Nov-2023 とか 16-Nov-2023のような0埋めされてない場合

+ [Help with Formula Field Creation](https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007Phi6jSAB)


```
IF(MID(Custom__c,3,1) ="-",
DATE(VALUE(RIGHT(Custom__c, 4)),
CASE(MID(Custom__c, 4, 3),"Jan", 1, "Feb", 2, "Mar", 3, "Apr", 4, "May", 5, "Jun", 6,"Jul", 7, "Aug", 8, "Sep", 9, "Oct", 10, "Nov", 11, "Dec", 12, 0),
VALUE(LEFT(Custom__c, 2)),

DATE(VALUE(RIGHT(Custom__c, 4)),
CASE(MID(Custom__c, 3, 3),"Jan", 1, "Feb", 2, "Mar", 3, "Apr", 4, "May", 5, "Jun", 6,"Jul", 7, "Aug", 8, "Sep", 9, "Oct", 10, "Nov", 11, "Dec", 12, 0),
VALUE(LEFT(Custom__c, 1))
) - Custom_Date_Field__c
```


# ADDMONTHSとの合わせ技

+ [Calculate future date based on number](https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T53dF)

```
CASE(MONTH(ADDMONTHS( Ending_Date__c  , Month_number__c )),
1,"January", 
2,"February", 
3,"March", 
4,"April",
5,"May",
6,"June", 
7,"July", 
8,"August",
9,"September",
10,"October",
11,"November",
"December") + " " + 
TEXT(YEAR((ADDMONTHS( Ending_Date__c  , Month_number__c ))))
```