plug-in code:
(function ($) {$.formatdate = function (pattern,date) {//If not set, defaults to the current time if (!date) date = new Date (); if (typeof (date) = = = " String ") {if (date==" ") Date = new Date (); else date = new Date (Date.replace (/-/g, "/"));} /* Complement 00*/var Tofixedwidth = function (value) {var result = 100+value; return result.tostring (). substring (1);};/ * Configuration */var options = {regeexp:/(yyyy| m+|d+|h+|m+|s+|ee+|ws?| p)/g,months: [' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' October ', ' Novembe ' R ', ' December '],weeks: [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday ']};/* time switch */var Swithhours = function (hours) {return hours<12? " AM ":" PM ";};/ * Configuration value */var pattrnvalue = {"yyyy":d ate.getfullyear (),//year "MM": Tofixedwidth (Date.getmonth () +1), Month "DD": Tofixedwidth (Date.getdate ()),//Date "HH": Tofixedwidth (Date.gethours ()),//Hour "MM": Tofixe Dwidth (Date.getminutes ()),//min "ss": Tofixedwidth (Date.getseconds ()), Second "ee": Options.months[date.getmonth ()],//month name "WS": Options.weeks[date.getday ()],//week name "M":d ate . GetMonth () +1, "D":d ate.getdate (), "H":d ate.gethours (), "M":d ate.getminutes (), "s":d ate.getsec Onds (), "P": Swithhours (Date.gethours ())};return Pattern.replace (Options.regeexp,function () {return pattrnvalue[a Rguments[0]];}) (JQuery);
Plugin use:
The premise is to have a jquery-1.8.3.js
<!--jquery Usage--
<script type= "Text/javascript" src= "jquery-1.8.3.js"/></script><script type= "Text/javascript" src= " Jquery.dateformat.js "></script><script type=" Text/javascript ">$ (function ($) {var time = new Date (" 2014-01-01 13:20:30 "); Console.info ($.formatdate ("Yyyy-mm-dd hh:mm:ss ws ee", "2013-1-1 13:20:30");}); </script>
Effect:
Code Resources Download