# 日付to文字列(Format,strftime,日付フォーマット)

# Bash

date -d "2016-11-01" +"%Y/%m/%d"
# day-of-week
date -d "2016-11-01" +%w

yyyy=`date +"%Y"`;
MMdd=`date +"%m%d"`;

# C#

yyyy/MM/dd hh:mm:ss.fff

# Excel

=TEXT(A1, "yyyy-mm-dd")
yyyy/mm/dd hh:mm:ss.000
# mm は大文字小文字区別しないが、文脈によって異なる?
Q. 日付から曜日を取得したい
A. こんな感じ
    =TEXT(WEEKDAY(A1), "aaa")  -> 月
    =TEXT(WEEKDAY(A1), "aaaa") -> 月曜日
    =TEXT(WEEKDAY(A1), "ddd")  -> Mon
    =TEXT(WEEKDAY(A1), "dddd") -> Monday

# JavaScript

標準では無いので、、

function zeroPad(v, n) {
    return ('0000000000' + v).slice(-n);
}
function formatDate(d) {
    return d.getFullYear()
        + "-" + zeroPad(d.getMonth() + 1, 2)
        + "-" + zeroPad(d.getDate(), 2);
}
function formatDateTime(d) {
    return formatDate(d)
        + " " + zeroPad(d.getHours(), 2)
        + ":" + zeroPad(d.getMinutes(), 2)
        + ":" + zeroPad(d.getSeconds(), 2)
        + "." + zeroPad(d.getMilliseconds(), 3);
}

# PHP

strftime('%Y/%m/%d %H:%M:%S', $t)
strftime('%Y/%m/%d %H:%M:%S', $t)  # [x] Microseconds : (can't get)
date('Y/m/d H:i:s.u', $t)          # [x] Microseconds : 000000
$dt->format('Y/m/d H:i:s.u')       # [o] Microseconds : 333333

# given this.
$t  = strtotime('2015-03-22 14:32:25.333333');
$dt = new DateTime('2015/03/22 14:32:25.333333');

// [o] %f , u : Microseconds ('u' only)
// [o] %a , D : Sat
// [o] %A , l : Saturday
// [x] %E     : Weekday on Locale -> not supported

gmdate('Y/m/d H:i:s', $ts); # GMT   <- *TODO*

# PowerShell

(Get-Date).ToString("Y/m/d H:M:s")

yyyyMMdd_HHmmss 形式だったような?

(Get-Date).ToString("F") # F??

# Python

dt.strftime('%Y/%m/%d %H:%M:%S.%f')
# [o] %f : Microseconds
# [o] %a : Sat
# [o] %A : Saturday
# [x] %E : Weekday on Locale -> not supported

# Ruby

d.strftime('%Y-%m-%d')
t.strftime('%Y-%m-%d %H:%M:%S')

# [x] %f : Microseconds -> not supported
# [o] %a : Sat
# [o] %A : Saturday
# [x] %E : Weekday on Locale -> not supported

# Microseconds
t.usec

# Japanese Weekday
class Time
  def wday_ja
    ['日', '月', '火', '水', '木', '金', '土', '日'][wday]
  end
end

# Swift

let fmt  = DateFormatter()
...
fmt.string(from: d)
let c:Calendar = Calendar(identifier: .gregorian)
let d:Date = c.date(from: DateComponents(year: 2017, month: 1, day: 23))!
let fmt  = DateFormatter()

// 和暦
fmt.locale = Locale(identifier: "ja_JP")
fmt.calendar = Calendar(identifier: .japanese)
fmt.dateFormat = "Gyy年M月d日(E)"
fmt.string(from: d) // => 平成29年1月23日(月)

// 西暦
fmt.locale = Locale(identifier: "ja_JP")
fmt.dateFormat = "yyyy/MM/dd (E)"
fmt.calendar = Calendar(identifier: .gregorian)
fmt.string(from: d) // => 2017/01/23 (月)


// 関数にした版
func formatDate(_ d: Date, format: String = "yyyy/MM/dd", identifier: Calendar.Identifier = .gregorian) -> String {
    let fmt = DateFormatter()
    fmt.locale = Locale(identifier: "ja_JP")
    fmt.dateFormat = format
    fmt.calendar = Calendar(identifier: identifier)
    return fmt.string(from: d)
}

[ 📩 ご意見 ]