# 文字列to時刻(ParseDateTime)

# PHP

$dt = new DateTime('2015/03/04 23:15:12.333333'); // DateTime class
$ts = strtotime('2015/03/14 23:15:12.333333');    // seconds from epoch (1970-01-01 00:00:00 GMT)  # int
$dt = new DateTime('2015/03/04 23:15:12.333333'); // DateTime class

# from format
$dt = DateTime::createFromFormat('Ymd_His.u', '20150322_143225.333333'); // not %Y%m%d..

# PowerShell

[System.DateTime]::('7:30')

# Python

datetime.datetime.strptime(str, fmt).date()
# Python 2.5 以降
def parsedate1(str, fmt = '%Y%m%d'):
  return datetime.datetime.strptime(str, fmt).date()

# Python 2.4   この * は何だろう?
def parsedate2(str, fmt = '%Y%m%d'):
  return datetime.date(*time.strptime(str, fmt)[0:3])


print parsedate1('20120125')
print parsedate2('20120125')


# 日付だけ
datetime.date(*time.strptime('%Y%m%d')[0:3]

dt.strptime('%Y%m%d', '20121005')

import datetime
d = datetime.datetime.strptime('2015-03-07', '%Y-%m-%d').date()                      # datetime.date class
t = datetime.datetime.strptime('2015-03-07 13:25:12.333333', '%Y-%m-%d %H:%M:%S.%f') # datetime.datetime class

# old
import time
d = datetime.date(*time.strptime('2015-03-07', '%Y-%m-%d')[0:3])

# Ruby

t = Time::parse('2015-03-07 13:25:12.333333')
require 'time'
d = Date::parse('2015-03-14')                 # => Date class
t = Time::parse('2015-03-07 13:25:12.333333') # => Time class

# Swift

fmt.date(from: s)
func parseDate(_ s: String) -> Date {
    let fmt = DateFormatter()
    fmt.dateFormat = "yyyy/MM/dd"
    fmt.locale = Locale(identifier: "ja_JP") // 不要?
    fmt.calendar = Calendar(identifier: .gregorian)
    return fmt.date(from: s)!
}

[ 📩 ご意見 ]