jquery Time Format Plugin

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.