/    Sign up×
Community /Pin to ProfileBookmark

DATE SCRIPT: make life easy

I had to create a little snippet to create a properly formatted date and so thought I would share it with you all.

[CODE]
//set up date variables
var today = new Date();
var month = today.getMonth();
var day_n = today.getDate();
var day = today.getDay();
var year = today.getFullYear();
//set up string arrays
var arrMonth = [‘January’,’February’,’March’,’April’,’May’,’June’,’July’,’August’,’September’,’October’,’November’,’December’];
var arrDay = [‘Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’,’Sunday’];
var suffix;
//create date suffix
if(day_n == ‘1’ || day_n == ’21’ || day_n == ’31’){
suffix = ‘st’};
else if(day_n == ‘2’ || day_n == ’22’){
suffix = ‘nd’};
else if(day_n == ‘3’ || day_n == ’23’){
suffix = ‘rd’};
else{
suffix = ‘th’};

var now = arrDay[day] + ‘, ‘ + arrMonth[month] + ‘ ‘ + day_n + suffix + ‘ ‘ + year;

[/CODE]

put this in your page header, not in a function!

then wherever you want it add <script>document.write(now)</script> and the formatted date will be printed.

the format is like this…. Thursday, December 15th 2005

nJoy!

~~CM!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@avaDec 16.2005 — Thanks, looks good
Copy linkTweet thisAlerts:
@felgallDec 16.2005 — <i>
</i>//create date suffix
if(day_n == '1' || day_n == '21' || day_n == '31'){
suffix = 'st'};
else if(day_n == '2' || day_n == '22'){
suffix = 'nd'};
else if(day_n == '3' || day_n == '23'){
suffix = 'rd'};
else{
suffix = 'th'};


can be more efficiently coded as

<i>
</i>//create date suffix
switch(date_n) {
case '1': case '21': case '31': suffix = 'st'; break;
case '2': case '22': suffix = 'nd'; break;
case '3': case '23': suffix = 'rd'; break;
default:suffix = 'th';}
Copy linkTweet thisAlerts:
@CrazyMerlinauthorDec 17.2005 — thx felgall

I started with a switch case, but took it out for some reason?

hmm!
×

Success!

Help @CrazyMerlin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...