Created
November 6, 2011 19:12
-
-
Save kokudori/1343329 to your computer and use it in GitHub Desktop.
DateFormat.jsをCoffeeScriptで書いてみた。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
DateFormat.jsは | |
http://yas-hummingbird.blogspot.com/2010/03/javascript-netdate.html | |
さんが書かれたJSのDateのtoString用パッチです。 | |
ライセンスは下記を参照ください。 | |
### | |
### | |
Javascript Date dateFormat | |
Copyright 2010 | |
Released under the MIT and GPL licenses. | |
@Author : y@s | |
@Version : 1.0 | |
@Published : 2010/03/24 | |
@LastUpdate : 2010/04/12 | |
@Demo : http://humming-bird.googlecode.com/svn/trunk/misc/dateFormat.html | |
### | |
clientLanguage = if navigator["language"]? | |
navigator["language"] | |
else | |
navigator["userLanguage"] | |
clientLanguage or= 'en' | |
pz = (n) -> | |
if n < 10 then '0' + n else n | |
toString = Date.prototype.toString | |
# fixed format string | |
fixedFormats = | |
d: | |
en: 'M/d/yyyy' | |
ja: 'yyyy/MM/dd' | |
D: | |
en: 'dddd, MMMM dd, yyyy' | |
ja: 'yyyy年M月d日' | |
f: | |
en: 'dddd, MMMM dd, yyyy h:mm tt' | |
ja: 'yyyy年M月d日 H:mm' | |
F: | |
en: 'dddd, MMMM dd, yyyy h:mm:ss tt' | |
ja: 'yyyy年M月d日 H:mm:ss' | |
g: | |
en: 'M/d/yyyy h:mm tt' | |
ja: 'yyyy/MM/dd H:mm' | |
G: | |
en: 'M/d/yyyy h:mm:ss tt' | |
ja: 'yyyy/MM/dd H:mm:ss' | |
m: | |
en: 'MMMM dd' | |
ja: 'M月d日' | |
M: | |
en: 'MMMM dd' | |
ja: 'M月d日' | |
### | |
Round-trip date/time pattern | |
o: | |
en: 'yyyy-MM-ddTHH:mm:ss.fffzzz' | |
ja: 'yyyy-MM-ddTHH:mm:ss.fffzzz' | |
### | |
r: | |
en: 'ddd, dd MMM yyyy HH:mm:ss GMT' | |
ja: 'ddd, dd MMM yyyy HH:mm:ss GMT' | |
R: | |
en: 'ddd, dd MMM yyyy HH:mm:ss GMT' | |
ja: 'ddd, dd MMM yyyy HH:mm:ss GMT' | |
s: | |
en: 'yyyy-MM-ddTHH:mm:ss' | |
ja: 'yyyy-MM-ddTHH:mm:ss' | |
t: | |
en: 'h:mm tt' | |
ja: 'H:mm' | |
T: | |
en: 'h:mm:ss tt' | |
ja: 'H:mm:ss' | |
u: | |
en: 'yyyy-MM-dd HH:mm:ssZ' | |
ja: 'yyyy-MM-dd HH:mm:ssZ' | |
### | |
UTC Time | |
U: | |
en: 'dddd, MMMM dd, yyyy h:mm:ss tt' | |
ja: 'yyyy年M月dd日 H:mm:ss' | |
### | |
y: | |
en: 'MMMM, yyyy' | |
ja: 'yyyy年M月' | |
Y: | |
en: 'MMMM, yyyy' | |
ja: 'yyyy年M月' | |
### | |
@param {date} date | |
@param {string} pattern | |
@param {string} language | |
@return {string} | |
### | |
format = (date, pattern, language) -> | |
week = [{ | |
en: 'Sunday' | |
ja: '日曜日' | |
},{ | |
en: 'Monday' | |
ja: '月曜日' | |
},{ | |
en: 'Tuesday' | |
ja: '火曜日' | |
},{ | |
en: 'Wednesday' | |
ja: '水曜日' | |
},{ | |
en: 'Thursday' | |
ja: '木曜日' | |
},{ | |
en: 'Friday' | |
ja: '金曜日' | |
},{ | |
en: 'Saturday' | |
ja: '土曜日' | |
}] | |
wsc = | |
en: 3 | |
ja: 1 | |
month = [ | |
'January' | |
'February' | |
'March' | |
'April' | |
'May' | |
'June' | |
'July' | |
'August' | |
'September' | |
'October' | |
'November' | |
'December' | |
] | |
format = /([yMd]{4}|[dfFMz]{3}|[dfFghHmMstyz]{2}|[hHz]|%?[dmsfFt]|%?M(?!T))/g | |
pattern.replace(format, ($1) -> | |
lang = language || clientLanguage | |
$1 = $1.replace('%','') | |
switch $1 | |
# Year | |
when 'yyyy' #The year in four digits | |
date.getFullYear() | |
when 'yy' #The year without the century | |
pz(date.getFullYear() + "") | |
# Month | |
when 'MMMM' #The full name of the month | |
m = date.getMonth() | |
if lang is 'en' then month[m] else "#{m+1}月" | |
when 'MMM' #The abbreviated name of the month | |
m = date.getMonth() | |
if lang is 'en' then month[m].slice(0, 3) else m+1 | |
# Numeric Month | |
when 'MM' | |
pz(date.getMonth() + 1) | |
when 'M' | |
date.getMonth() + 1 | |
# Week | |
when 'dddd' #Week full name | |
week[date.getDay()][lang]; | |
when 'ddd' #Week short name | |
week[date.getDay()][lang].slice(0, wsc[lang]) | |
# Day - The day of the month | |
when 'dd' | |
pz(date.getDate()) | |
when 'd' | |
date.getDate() | |
# Hour 24 | |
when 'HH' | |
pz(date.getHours()) | |
when 'H' | |
date.getHours() | |
# Hour 12 | |
when 'hh' | |
time = if tmp = date.getHours() % 12 then tmp else 12 | |
pz(time) | |
when 'h' | |
if tmp = date.getHours() % 12 then tmp else 12 | |
# Minute | |
when 'mm' | |
pz(date.getMinutes()) | |
when 'm' | |
date.getMinutes() | |
# Second | |
when 'ss' | |
pz(date.getSeconds()) | |
when 's' | |
date.getSeconds() | |
# Millisecond | |
when 'fff', 'ff', 'f' | |
('00' + date.getMilliseconds()).slice(-3).slice(0, $1.length) | |
when 'FFF', 'FF', 'F' | |
tmp = date.getMilliseconds() | |
('00' + tmp) | |
.slice(-3) | |
.slice(0, $1.length) | |
.replace(/0+$/, (z) -> | |
if tmp.length > 1 then '' else z | |
) | |
# AM/PM | |
when 'tt' | |
if date.getHours() < 12 then 'AM' else 'PM' | |
when 't' | |
if date.getHours() < 12 then 'A' else 'P' | |
# GMT | |
when 'zzz' | |
tmp = -date.getTimezoneOffset() / 60; | |
sign = if tmp >= 0 then '+' else '' | |
sign + pz(tmp) + ':00' | |
when 'zz' | |
tmp = -date.getTimezoneOffset() / 60; | |
sign = if tmp >= 0 then '+' else '' | |
sign + pz(tmp); | |
when 'z' | |
tmp = -date.getTimezoneOffset() / 60; | |
sign = if tmp >= 0 then '+' else '' | |
sign + tmp; | |
# The period or era | |
# Anno Domini - A.D. | |
# Before Christ - B.C. | |
when 'gg' | |
if lang is 'en' then 'D.C.' else '西暦' | |
else | |
$1 | |
) | |
### | |
@param {string} pattern | |
@param {string} language | |
@return {string} | |
### | |
Date.prototype.toString = (pattern, language) -> | |
if pattern is undefined | |
return toString.call(this, arguments) | |
lang = if pattern.match(/^r$/i) then 'en' else language || clientLanguage | |
if pattern.match(/^[dDfFgGmMrRstTuyY]$/) | |
format(this, fixedFormats[pattern][lang], lang) | |
else | |
format(this, pattern, lang) || toString.call(this, arguments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment