As a follow-up to my last post about Javascript date functions, I went ahead and implemented strftime for javascript.
Have a look at the demo at the link above, and download the code: strftime.js. It's distributed under a BSD license, so use it and give me feedback. Post comments here for now.The code is documented using doxygen style comments.
You should also check out Stoyan's time input library that lets you do the equivalent of php's strtotime function.
Update: This code is now part of the YUI library and all further development will be done there.
/bb|[^b]{2}/
Never stop Grokking
Tuesday, April 22, 2008
Javascript date functions
I keep forgetting what I can do with dates in javascript, so this post is just a list of built in date functions. I've also seen a lot of sites that do some terrible things with javascript dates including trying to parse it out of a string, or using getYear(). Don't do that.
I've also seen sites that contain arrays of calendar month names and days of the week. This is locale specific, so do it only if you're willing to make the array easily localised.
To create a new date object:
Also don't forget the
After this line,
One useful method is
A useful page for javascript date functions is quackit.com
I mentioned earlier that you should not use
I'd always thought about writing an equivalent to
Update: I've built an implementation of strftime in javascript. This implementation is also part of the YUI library.
I've also seen sites that contain arrays of calendar month names and days of the week. This is locale specific, so do it only if you're willing to make the array easily localised.
To create a new date object:
var d = new Date;
var d = new Date('2008/04/22');
var d = new Date('2008/04/22 01:03:31');
var d = new Date(1208758100000);Note: don't type that into firefox/firebug while editing a post on blogger - they have a global variable called d which is used to save your post.Also don't forget the
new keyword. Without that you'll just create a string containing the current date.After this line,
d evaluates to a string representation of the date which isn't very useful for anything since it's almost always not what you want. You really want to get various parts of the date out. There's a whole bunch of functions to do that:d.getYear(); // deprecated function to get year. don't use this d.getFullYear(); // get four digit year. use this d.getMonth(); // get the month 0 - 11 d.getDate(); // get the day of the month 1 - 31 d.getDay(); // get the day of the week 0-6 (Sunday is 0) d.getHours(); // get the hour - 0-23. Note the s in the function name d.getMinutes(); // get the minutes of the hour - 0-59 d.getSeconds(); // get the seconds of the minute - 0-59 d.getMilliseconds(); // get milliseconds past the second - 0-999 d.getTime(); // get number of milliseconds since the epoch. integer divide by 1000 to get unix timestamp d.getTimezoneOffset(); // get offset from GMT in minutes - note capitalisationAll of these functions return the value for your current timezone. To get values in UTC, add
UTC after the get in the method name. There are also equivalent set methods, but I've never needed to use them.One useful method is
parse() which takes in a string representation of a date and returns the number of milliseconds since the epoch for that. You can call this directly on the Date class.A useful page for javascript date functions is quackit.com
I mentioned earlier that you should not use
getYear();. This is so because this method is not quite Y2K compliant. It is sort of compliant, but browsers don't implement it in the same way. The standard says that it should return the number of years since 1900, which means 2000 would be 100 and 2008 would be 108. Netscape/Firefox does that correctly (possibly because they defined the standard back in the day), but IE decided to 'fix' it by returning 4 digit years for anything past 1999, which means that 1999 is 99 while 2000 is 2000. Your code gets unwieldy trying to manage it, so it's best to just use getFullYear().I'd always thought about writing an equivalent to
strftime for javascript, but never got down to it. In my mind, it would be something that uses a regex with a callback function, and I'd use the unique property of javascript that allows one to call an object's methods like keys in an associative array. Basically something like this:var fmt = {'Y': 'getFullYear', 'm': 'getMonth', 'd': 'getDate'};
Date.prototype.strftime = function(str)
{
var d = this;
var ret = str.replace(/%([Ymd])/g, function(m0, m1)
{
return d[fmt[m1]]();
});
d=null;
return ret;
};
I'll have to build on that sometime.Update: I've built an implementation of strftime in javascript. This implementation is also part of the YUI library.
Labels
2fa
4.01-strict
404
accessibility
acer
airport wifi
algorithm
android
apache
API
att
audio
australia
authentication
ayttm
badges
bandwidth
bbc
bcp
berlin
blog
blogger
blogger template
bof
book
boomerang
broken
bug
byte order
c
caching
chrome
closure
cmc
cms
codepo8
colours
comic strip
comments
communication
compile
conference
confoo
cookies
couchdb
cracker
crash
creative
credit card
crockford
cron
csrf
css
curl
data tags
database
date
db
delicious
design
developer
dhtml
dom
dopplr
dragdrop
dynamic script node
education
email
endianness
entities
ephemeral ports
epicondylitis
error checking
esmtp
everybuddy
extensions
facebook
favicon
fc9
fedora
fidelity
filesystem
firefox
firesheep
flickr
flot
form
forms
fosdem
foss
foss.in
freebsd
freedom
freestyle
function currying
gdb
geek
geo
george
gmail
gnome
google
gradient
groupon
hack
hacker
hardy
hash
howtos
htc
html
html5
http
i18n
icici
ie
iit
im
innerHTML
instant messaging
interfaces
internet
ios
ip
ip address
ipc
iphone
ipv6
iso8601
jabber
javascript
json
keynote
latency
latex
LC_TIME
linux
load
localisation
login
lsm
luhn
MAC
macosx
mail
mathematics
mathjax
measurement
media queries
meetup
memory
messaging
microformats
missing kids
mobile
montreal
movable type
mp3
mvc
mysql
name generator
network
nexus
nodejs
notes
opensource
opera
partition
passwords
pdf
performance
perl
phone
php
planet
png
ports
prerender
printing
privacy
programming
programming style
progressive enhancement
puzzle
recovery
redhat
regex
regular expressions
review
rfc2822
rfc3339
rhel
roundtrip
rss
safari
sampling
scalability
scripting
secnet
security
sed
segfault
self extracting tarball
sendmail
server
shell
shell script
sigdashes
site
slideshare
smtp
soap
sockets
spoofing
ssl
starttls
startup
statistics
stoyan
strftime
stubbornella
sydney
sysadmin
tablespace
talks
tcp
testing
theme
thisisbroken
thisisfixed
thoughts
tim berners-lee
timezone
tips
toc
toy
transactions
twitter
two factor auth
typing
ubuntu
ui
unicode
unix
url
usability
velocity
vint cerf
w3c
wav
web
web services
webcam
webdev
webdu
webkit
webtiming
whois
widgets
wifi
workaround
write performance
X
xss
yahoo
ydn
YQL
yslow
yui
Translate this page
Twitter updates
- PHOTO FEED
- Blog feed
- © PHILIP TELLIS 2012
-

The other side of the moon by Philip Tellis is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.