Skip to content

Commit cea373d

Browse files
author
Spetnik
committed
Version 0.2.0
- Added support for %D, %f (milliseconds, not microseconds), %%, and %%. - Changed replace loop to regex replace function
1 parent 34c37c4 commit cea373d

File tree

4 files changed

+258
-18
lines changed

4 files changed

+258
-18
lines changed

.gitignore

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
#################
2+
## SublimeText
3+
#################
4+
*.sublime-project
5+
*.sublime-workspace
6+
7+
#################
8+
## Eclipse
9+
#################
10+
11+
*.pydevproject
12+
.project
13+
.metadata
14+
bin/
15+
tmp/
16+
*.tmp
17+
*.bak
18+
*.swp
19+
*~.nib
20+
local.properties
21+
.classpath
22+
.settings/
23+
.loadpath
24+
25+
# External tool builders
26+
.externalToolBuilders/
27+
28+
# Locally stored "Eclipse launch configurations"
29+
*.launch
30+
31+
# CDT-specific
32+
.cproject
33+
34+
# PDT-specific
35+
.buildpath
36+
37+
38+
#################
39+
## Visual Studio
40+
#################
41+
42+
## Ignore Visual Studio temporary files, build results, and
43+
## files generated by popular Visual Studio add-ons.
44+
45+
# User-specific files
46+
*.suo
47+
*.user
48+
*.sln.docstates
49+
50+
# Build results
51+
52+
[Dd]ebug/
53+
[Rr]elease/
54+
x64/
55+
build/
56+
[Bb]in/
57+
[Oo]bj/
58+
59+
# MSTest test Results
60+
[Tt]est[Rr]esult*/
61+
[Bb]uild[Ll]og.*
62+
63+
*_i.c
64+
*_p.c
65+
*.ilk
66+
*.meta
67+
*.obj
68+
*.pch
69+
*.pdb
70+
*.pgc
71+
*.pgd
72+
*.rsp
73+
*.sbr
74+
*.tlb
75+
*.tli
76+
*.tlh
77+
*.tmp
78+
*.tmp_proj
79+
*.log
80+
*.vspscc
81+
*.vssscc
82+
.builds
83+
*.pidb
84+
*.log
85+
*.scc
86+
87+
# Visual C++ cache files
88+
ipch/
89+
*.aps
90+
*.ncb
91+
*.opensdf
92+
*.sdf
93+
*.cachefile
94+
95+
# Visual Studio profiler
96+
*.psess
97+
*.vsp
98+
*.vspx
99+
100+
# Guidance Automation Toolkit
101+
*.gpState
102+
103+
# ReSharper is a .NET coding add-in
104+
_ReSharper*/
105+
*.[Rr]e[Ss]harper
106+
107+
# TeamCity is a build add-in
108+
_TeamCity*
109+
110+
# DotCover is a Code Coverage Tool
111+
*.dotCover
112+
113+
# NCrunch
114+
*.ncrunch*
115+
.*crunch*.local.xml
116+
117+
# Installshield output folder
118+
[Ee]xpress/
119+
120+
# DocProject is a documentation generator add-in
121+
DocProject/buildhelp/
122+
DocProject/Help/*.HxT
123+
DocProject/Help/*.HxC
124+
DocProject/Help/*.hhc
125+
DocProject/Help/*.hhk
126+
DocProject/Help/*.hhp
127+
DocProject/Help/Html2
128+
DocProject/Help/html
129+
130+
# Click-Once directory
131+
publish/
132+
133+
# Publish Web Output
134+
*.Publish.xml
135+
*.pubxml
136+
137+
# NuGet Packages Directory
138+
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
139+
#packages/
140+
141+
# Windows Azure Build Output
142+
csx
143+
*.build.csdef
144+
145+
# Windows Store app package directory
146+
AppPackages/
147+
148+
# Others
149+
sql/
150+
*.Cache
151+
ClientBin/
152+
[Ss]tyle[Cc]op.*
153+
~$*
154+
*~
155+
*.dbmdl
156+
*.[Pp]ublish.xml
157+
*.pfx
158+
*.publishsettings
159+
160+
# RIA/Silverlight projects
161+
Generated_Code/
162+
163+
# Backup & report files from converting an old project file to a newer
164+
# Visual Studio version. Backup files are not needed, because we have git ;-)
165+
_UpgradeReport_Files/
166+
Backup*/
167+
UpgradeLog*.XML
168+
UpgradeLog*.htm
169+
170+
# SQL Server files
171+
App_Data/*.mdf
172+
App_Data/*.ldf
173+
174+
#############
175+
## Windows detritus
176+
#############
177+
178+
# Windows image file caches
179+
Thumbs.db
180+
ehthumbs.db
181+
182+
# Folder config file
183+
Desktop.ini
184+
185+
# Recycle Bin used on file shares
186+
$RECYCLE.BIN/
187+
188+
# Mac crap
189+
.DS_Store
190+
191+
192+
#############
193+
## Python
194+
#############
195+
196+
*.py[co]
197+
198+
# Packages
199+
*.egg
200+
*.egg-info
201+
dist/
202+
build/
203+
eggs/
204+
parts/
205+
var/
206+
sdist/
207+
develop-eggs/
208+
.installed.cfg
209+
210+
# Installer logs
211+
pip-log.txt
212+
213+
# Unit test / coverage reports
214+
.coverage
215+
.tox
216+
217+
#Translations
218+
*.mo
219+
220+
#Mr Developer
221+
.mr.developer.cfg

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ Formatting specifiers:
2525
%a Abbreviated weekday name (Sun..Sat)
2626
%b Abbreviated month name (Jan..Dec)
2727
%c Month, numeric (0..12)
28-
%D* Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
28+
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
2929
%d Day of the month, numeric (00..31)
3030
%e Day of the month, numeric (0..31)
31-
%f* Microseconds (000000..999999)
31+
%f** Milliseconds (000..999)
3232
%H Hour (00..23)
3333
%h Hour (01..12)
3434
%I Hour (01..12)
@@ -53,7 +53,8 @@ Formatting specifiers:
5353
%x* Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
5454
%Y Year, numeric, four digits
5555
%y Year, numeric (two digits)
56-
%%* A literal “%” character
57-
%x x, for any “x” not listed above
56+
%% A literal “%” character
57+
%? ?, for any “?” not listed above
5858

59-
* Not yet supported
59+
* Not yet supported
60+
** The MySQL DATE_FORMAT() %f specifier returns Microseconds (000000..999999)

formatdate.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ var formatDate = function(date, format, utc){
99

1010
var formats = {
1111
'%a': function(){return ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][utc ? date.getUTCDay() : date.getDay()];},
12-
'%b': function(){
13-
return ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][utc ? date.getUTCMonth() : date.getMonth()];
14-
},
12+
'%b': function(){return ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][utc ? date.getUTCMonth() : date.getMonth()];},
1513
'%c': function(){return utc ? date.getUTCMonth() + 1 : date.getMonth() + 1;},
16-
'%D': function(){/*Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)*/},
14+
'%D': function(){
15+
var t, n=this["%e"](), nn=parseInt(n.toString().substr(-2));
16+
return nn>3&&nn<21?n+"th":t=["th","st","nd","rd"][n.toString().substr(-1)],n+(t==null?"th":t);
17+
},
1718
'%d': function(){return pad(utc?date.getUTCDate():date.getDate(),2);},
1819
'%e': function(){return utc?date.getUTCDate():date.getDate();},
19-
'%f': function(){/*milliseconds, not microseconds*/return utc?date.getUTCMilliseconds():date.getMilliseconds();},
20+
'%f': function(){return pad(utc?date.getUTCMilliseconds():date.getMilliseconds(),3);},
2021
'%H': function(){return pad(this["%k"](), 2);},
2122
'%h': function(){return pad(this["%l"](), 2);},
2223
'%I': function(){return this["%h"]();},
@@ -30,7 +31,7 @@ var formatDate = function(date, format, utc){
3031
'%r': function(){return this["%h"]() + ":" + this["%i"]() + ":" + this["%S"]() + " " + this["%p"]()},
3132
'%S': function(){return pad(utc?date.getUTCSeconds():date.getSeconds(),2);},
3233
'%s': function(){return this["%S"]();},
33-
'%T': function(){return this["%h"]()+":"+this["%i"]()+this["%S"]()+" "+this["%p"]();},
34+
'%T': function(){return this["%H"]()+":"+this["%i"]()+":" + this["%S"]();},
3435
'%U': function(){/*Week (00..53), where Sunday is the first day of the week*/},
3536
'%u': function(){/*Week (00..53), where Monday is the first day of the week*/},
3637
'%V': function(){/*Week (01..53), where Sunday is the first day of the week; used with %X*/},
@@ -40,13 +41,17 @@ var formatDate = function(date, format, utc){
4041
'%X': function(){/*Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V*/},
4142
'%x': function(){/*Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v*/},
4243
'%Y': function(){return utc?date.getUTCFullYear():date.getFullYear();},
43-
'%y': function(){return (utc?date.getUTCFullYear():date.getFullYear()).toString().substring(2);}
44+
'%y': function(){return (utc?date.getUTCFullYear():date.getFullYear()).toString().substring(2);},
45+
'%%': function(){return "%"}
4446
};
4547

46-
var output = format;
48+
var replacer = function(match){
49+
if(formats[match] == null)
50+
return match.substr(1,1);
4751

48-
for(var f in formats){
49-
output = output.replace(f, formats[f]());
52+
return formats[match]();
5053
}
51-
return output;
54+
55+
return format.replace(/\%.?/g, replacer);
56+
5257
};

sample.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
button{width:120px;height:40px;background-color:#fff;color:#047;border:1px solid #047;border-radius:4px;font-weight:bold;cursor:pointer;}
88
button:hover{background-color:#047;color:#fff;}
99
table{margin:0px auto 0px auto;}
10-
td>div{margin:2px 10px;height:30px;padding-top:6px;width:250px;border:2px dashed #ddd;text-align: center;}
10+
td>div{margin:2px 10px;height:30px;padding-top:6px;width:280px;border:2px dashed #ddd;text-align: center;}
1111
</style>
1212
<script type="text/javascript" src="formatdate.js"></script>
1313
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
1414
<script type="text/javascript">
1515
$(function(){
16+
//Set DIV text to formatted current date according to specifier text of corresponding BUTTON
1617
$('button').click(function(){var $button=$(this);$($('tr').filter(function(){return $(this).index()==$button.parent().parent().index();})).find("td>div").text(
1718
formatDate(new Date(),$(this).text())
1819
);})
@@ -29,7 +30,7 @@
2930
</tr>
3031
<tr>
3132
<td>
32-
<button>%W, %M %c, %Y</button>
33+
<button>%W, %M %D, %Y</button>
3334
</td>
3435
<td><div></div></td>
3536
</tr>
@@ -51,6 +52,18 @@
5152
</td>
5253
<td><div></div></td>
5354
</tr>
55+
<tr>
56+
<td>
57+
<button>%T</button>
58+
</td>
59+
<td><div></div></td>
60+
</tr>
61+
<tr>
62+
<td>
63+
<button>%g %%c%%%c</button>
64+
</td>
65+
<td><div></div></td>
66+
</tr>
5467

5568
</table>
5669
</body>

0 commit comments

Comments
 (0)