日付関数
関数
| 日付関数 | 説明 | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| date(year, month, day) | 日付を返します。日付の 3 つのディメンションを年、月、日の順序で指定します。次に例を示します。 |
||||||||||||||||||||||
| date_diff(datepart,startdate,enddate) | 2 つの日付間で��過した間隔を表す整数を返します。
datepart は、計算する間隔要素を示します。
enddate は終了日を示します。 2 つの日付の差は、指定された日付要素の違いに基づいて計算されます。たとえば、2 つの日付の年の差は、enddate の年要素から startdate の年要素が差し引かれて計算されます。date_diff("year", toDate("31-12-2015", "dd-MM-yyyy"), toDate("1-1-2016", "dd-MM-yyyy")) の結果は 1 になります。 同じように、日付要素の月を使用した例を次に示します。date_diff("month", toDate("31-12-2015", "dd-MM-yyyy"), toDate("1-1-2016", "dd-MM-yyyy")) の結果も 1 です。 startdate が enddate より後である場合、結果は差を表す、負の整数になります。例:
クエリの例: 無効な例: |
||||||||||||||||||||||
| date_to_epoch(date) | 日付をエポック秒に変換します。これは toDate(epoch_seconds) 関数の逆です。 1970 年 1 月 1 日 00:00:00.000 GMT からの秒数を返します。これより前の日付が渡された場合、結果は負の数値になります。パラメータが日付でない場合、結果はエラーになります。パラメータとして null が渡された場合、null が返されます。 例: |
||||||||||||||||||||||
| date_to_string(date, formatString) | 日付を文字列に変換します。 この関数は、最初の引数として toDate() または now() 関数を取る必要があります。
|
||||||||||||||||||||||
| dateRange(startArray_y_m_d, endArray_y_m_d) | 固定日付範囲を返します。最初のパラメータは、範囲の開始日を指定する配列です。2 番目のパラメータは、範囲の終了日を指定する配列です。以下に例を示します。 |
||||||||||||||||||||||
| day_in_month(date) | 特定の日付がその月の何日目かを表す整数を返します。使用法については day_in_week を参照してください。 | ||||||||||||||||||||||
| day_in_quarter(date) | 特定の日付がその四半期の何日目かを表す整数を返します。使用法については day_in_week を参照してください。 | ||||||||||||||||||||||
| day_in_week(date) | 特定の日付の曜日を表す整数を返します。1 = 日曜日、2 = 月曜日というようになります。 date は参照日を示します。 例:
|
||||||||||||||||||||||
| day_in_year(date) | 特定の日付がその年の何日目かを表す整数を返します。使用法については day_in_week を参照してください。 | ||||||||||||||||||||||
| daysBetween(date1, date2) | 2 つの日付間の日数を整数で返します。
daysBetween() 関数は、ディメンションを直接引数として取ることはできません。toDate() および now() 関数を引数として渡します。
|
||||||||||||||||||||||
| month_days(date) | 特定の日付に対応する月の日数を返します。 date は参照日を示します。 例:
クエリの例: 無効な例: |
||||||||||||||||||||||
| month_last_day(date) | 特定の日付に対応する月の最終日の日付を返します。使用法については week_last_day を参照してください。 | ||||||||||||||||||||||
| now() | UTC で現在の日時を返します。この関数は、foreach ステートメントでのみ有効です。一般的に、この関数は daysBetween() および toString() 関数で使用されます。 |
||||||||||||||||||||||
| quarter_days(date) | 特定の日付に対応する四半期の日数を返します。使用法については month_days を参照してください。 | ||||||||||||||||||||||
| quarter_last_day(date) | 特定の日付に対応する四半期の最終日の日付を返します。使用法については week_last_day を参照してください。 | ||||||||||||||||||||||
| toDate(string [,formatString]) | 文字列を日付に変換します。formatString 引数が指定されていない場合は yyyy-MM-dd HH:mm:ss 形式が使用されます。多くの場合、この関数は daysBetween() または toString() に引数として渡されます。 |
||||||||||||||||||||||
| toDate(epoch_seconds) | UNIX のエポック秒を日付に変換します。epoch_seconds が 0 の場合、toDate(epoch_seconds) は '1970-01-01 00:00:00' を返します。 この関数は、日付への期間の加算または日付からの期間の減算を行う場合に便利です。日付のタイムゾーンの違いを調整するときに時差を秒数で加算または減算すると、正しいローカル日付が算出されます。その時間がローカル子午線を越えている場合は、別の日付が算出されます。 たとえば、Current_Date が '1970-01-01 00:00:00' からの秒数で表される現在日付の場合、関数 toDate(Current_Date - 8*3600) は 8 時間を減算します。実例については、「タイムゾーンの使用」を参照してください。 |
||||||||||||||||||||||
| toString(date, formatString) | 日付を文字列に変換します。 この関数は、最初の引数として toDate() または now() 関数を取る必要があります。
|
||||||||||||||||||||||
| week_last_day(date) | 特定の日付に対応する週の最終日の日付を返します。 date は参照日を示します。 例:
クエリの例: 無効な例: |
||||||||||||||||||||||
| year_days(date) | 特定の日付に対応する年の日数を返します。使用法については month_days を参照してください。 | ||||||||||||||||||||||
| year_last_day(date) | 特定の日付に対応する年の最終日の日付を返します。使用法については week_last_day を参照してください。 |
固定日付範囲の指定
固定日付の範囲を指定するには、dateRange() 関数を使用します。日付を年、月、日の順序で指定します。
例
1a = filter a by date('year', 'month', 'day') in [dateRange([1970, 1, 1], [1970, 1, 11])];相対日付範囲の指定
相対日付範囲を指定するには、相対日付キーワードを含む配列で in 演算子を使用します。次に、4 つの例を示します。
1a = filter a by date('year', 'month', 'day') in ["1 year ago".."current year"];
2a = filter a by date('year', 'month', 'day') in ["2 quarters ago".."2 quarters ahead"];
3a = filter a by date('year', 'month', 'day') in ["4 months ago".."1 year ahead"];
4a = filter a by date('year', 'month', 'day') in ["2 fiscal_years ago".."current day"];- current day
- n day(s) ago
- n day(s) ahead
- current week
- n week(s) ago
- n week(s) ahead
- current month
- n month(s) ago
- n month(s) ahead
- current quarter
- n quarter(s) ago
- n quarter(s) ahead
- current fiscal_quarter
- n fiscal_quarter(s) ago
- n fiscal_quarter(s) ahead
- current year
- n year(s) ago
- n year(s) ahead
- current fiscal_year
- n fiscal_year(s) ago
- n fiscal_year(s) ahead
| 相対日付値キーワード | 開始日 | 終了日 |
|---|---|---|
| current day | 2014/12/16 00:00:00 | 2014/12/16 23:59:59 |
| current quarter | 2014/10/1 00:00:00 | 2014/12/31 23:59:59 |
| 1 year ago | 2013/1/1 00:00:00 | 2013/12/31 23:59:59 |
| 1 month ahead | 2015/1/1 00:00:00 | 2015/1/31 23:59:59 |
| current fiscal_year | 2014/2/1 00:00:00 | 2015/1/31 23:59:59 |
| current fiscal_quarter | 2014/11/1 00:00:00 | 2015/1/31 23:59:59 |
| 2 fiscal_quarters ahead | 2015/5/1 00:00:00 | 2015/7/31 23:59:59 |
| current day - 1 year | 2013/12/16 00:00:00 | 2013/12/16 23:59:59 |
| current fiscal_year + 5 days | 2014/2/6 00:00:00 | 2014/2/6 23:59:59 |
日付の加算と減算
相対日付キーワードを使用して、日付の加算と減算を実行できます。
例
次に、加算と減算を使用した相対日付キーワードの時間枠の例を示します。これらの時間枠の例では、現在の日付は 2014/12/16、FiscalMonthOffset 1 (会計年度は 2 月 1 日に開始) です。
1a= filter a by date('year', 'month', 'day') in ["current day - 1 year"..] ;1a= filter a by date('year', 'month', 'day') in ["current day".."2 years ahead + 3 months"];1a= filter a by date('year', 'month', 'day') in ["current fiscal_year + 5 days".."2 years ahead + 3 months"];無制限の相対日付範囲の使用
例
1a = filter a by date('year','month','day') in [.."current month"];例
1q = filter q by OrderDate in [“2015-01-01"..];タイムゾーンの使用
toDate() 関数の実用的な用途は、Analytics ダッシュボードでのタイムゾーンの変更を計算することです。次の JSON コードフラグメントでは、変換に computeExpression アクションを使用し、saqlExpression を使用して toDate() 関数をコールします。この方法により、ローカルまたは UTC の最も適切な日時をダッシュボードに表示できます。
1"Extract_Opportunity": {
2 "action": "computeExpression",
3 "parameters": {
4 "source": "Digest_Opportunity",
5 "mergeWithSource": true,
6 "computedFields": [
7 {
8 "name": "CreatedDateNew",
9 "type": "Date",
10 "format": "MM/dd/yyyy",
11 "saqlExpression": "toDate(CreatedDate_sec_epoch - 8*3600)"
12 }
13 ]
14 }
15},この例は、既存の日付 CreatedDate_sec_epoch を取得し、8 時間を減算して新しい日付 CreateDateNew を作成しています。次の表に、この計算によって (形式が適用された) CreatedDateNew 日付がどのように変更されるかを示します。いずれの場合も、時間の変更に伴い日付も変更されています。
| CreatedDate_sec_epoch | CreatedDateNew |
|---|---|
| 2015-11-03T06:49:25.00OZ | 11/2/2015 |
| 2014-08-19T06:42:33.00OZ | 8/18/2014 |
| 2014-09-28T03:12:25.00OZ | 9/27/2014 |
詳細は、「computeExpression 変換」 トピックを参照してください。