SnippetCollection
Forums  
Links
 
Support
 


snippetCollection.com Forum Index FAQ Memberlist Search Usergroups Profile Log in to check your private messages Log in Register
Welcome to SnippetCollection.com, a friendly and fast growing community of programmers, coders, web application developers and more. You are currently viewing our forums as a guest!

This message will go away once you are registered. Also, by registering you will have access to all post topics, communicate privately with other members (PM), respond to polls and access other special features. Registration is fast, simple and absoultely free so please Click Here to join our community

Enjoy our great collection of snippets and tutorials!

Log in
Username:
Password:

 

Our Sponsors
           
Want to list your business here. Contact us.
 formatDate() function View next topic
View previous topic
Post new topic Reply to topic
Author Message
chrishirst



Joined: 04 Sep 2005
Posts: 22

PostPosted: Wed Apr 05, 2006 12:19 am Reply with quoteBack to top

Function to format the date, it will return the date in the format and order passed as a parameter.

d/m/y will return 5/4/2006 for example
d will return the day in the month
dd will return the short dayname + the numeric (Tue 5th)
dddd will return the long dayname + numeric (Tuesday 5th)

M will return the month number
MM will return the short month name (Apr)
MMMM will return the full month name (April)

useage

formatDate(dateString,FormatString)


Code:

<script type="text/javascript">
var monthNamesLong=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var monthNamesShort=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var dayNamesLong=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var dayNamesShort=new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

function formatDate(cDate,format) {
var dDate = new Date(cDate)
var wDay = dDate.getDay();
var dDay = dDate.getDate();
var dMonth = dDate.getMonth();
var dYear = dDate.getFullYear();
var patt = new RegExp("/","gi")
switch(dDay) {
   case 1 :
   var dSuffix = "st";
   break;
   case 21 :
   var dSuffix = "st";
   break;
   case 31 :
   var dSuffix = "st";
   break;
   case 2:
   var dSuffix = "nd";
   break;
   case 22:
   var dSuffix = "nd";
   break;
   case 3 :
   var dSuffix = "rd";
   break;
   default :
   var dSuffix = "th";
}
format = format.replace("y",dYear);

if (format.match("dd")!= null || format.match("MM") != null) {
   format = format.replace(patt, " ");
format = format.replace("dddd",dayNamesLong[wDay]+" "+dDay+dSuffix);
format = format.replace("dd",dayNamesShort[wDay]+" "+dDay+dSuffix);
format = format.replace("MMMM",monthNamesLong[dMonth]);
format = format.replace("MM",monthNamesShort[dMonth]);
} else {
format = format.replace("d",dDay);
format = format.replace("M",dMonth + 1);
}
return format
}
</script>
Display posts from previous:      
Post new topic Reply to topic


 Jump to:   



View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group :: FI Theme
All times are GMT