Jst[javascript Template] Uniform time format through custom identifiers

Source: Internet
Author: User
Tags getdate

The time that the database is fetched to do is generally: Thu Nov 18:29:21 gmt+0800 This format

When using JST to dynamically render the template, this will be output directly to the page by string, so you need to convert the time into a normal 2010-10-10 format

There are two ways to solve, one is by the outside of the JS conversion method as a parameter in the form of directly into the rendering parameters, such as:

var change = function (time) {...} var param = {' Change ': change};

So in the JST template, you need to write

{var timestr = change (_item.date)} ${timestr}

Another recommended way to do this is to use the JS method as a custom identifier, so that the JST template requires only the following:

${_item.date|changetime}

You can complete the automatic conversion, the specific JS code:

var mymodifiers = {Timeformat:function (thisvalue) {return changestrtoformattimestr (Thisvalue);}}; var arr = function (ARG) {return getloops (arg,true);} var param = {' result ': result, ' func ': arr}; if (Null = result) {param = {"Result": "", "Func": arr};} Param._modifiers = mymodifiers; $ ("Commentshowdiv"). InnerHTML = Trimpath.parsetemplate ($ ("commentdivhtml"). Value). process (param);

 //converts a time series to a specific Format function Changestrtoformattimestr (TIMESTR) {return formattime totime (timestr), "Yyyy-mm-dd HH: MM "); }//String converted to TIME function ToTime (s) {var converted = Date.parse (s); var mydate = new Date (converted); if (isNaN (mydate)) {VA R arys= S.split ('-'); MyDate = new Date (Arys[0],--arys[1],arys[2]); return mydate; }//Time format conversion function Formattime (DATE,FORMATSTR) {var str = formatstr; var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ']; STR=STR.R Eplace (/yyyy| Yyyy/,date.getfullyear ()); Str=str.replace (/yy| yy/, (date.getyear ()%) >9? (Date.getyear ()%). ToString (): ' 0 ' + (date.getyear ()% 100)); Str=str.replace (/mm/,date.getmonth () >9?date.getmonth (). toString (): ' 0 ' + date.getmonth ()); Str=str.replace (/m/g,date.getmonth ()); Str=str.replace (/w| W/g,week[date.getday ()]); Str=str.replace (/dd| Dd/,date.getdate () >9?date.getdate (). toString (): ' 0 ' + date.getdate ()); Str=str.replace (/d| D/g,date.getdate ()); Str=str.replace (/hh| Hh/,date.gethours () >9?date.gethours (). toString (): ' 0 ' + date.GetHours ()); Str=str.replace (/h| H/g,date.gethours ()); Str=str.replace (/mm/,date.getminutes () >9?date.getminutes (). toString (): ' 0 ' + date.getminutes ()); Str=str.replace (/m/g,date.getminutes ()); Str=str.replace (/ss| Ss/,date.getseconds () >9?date.getseconds (). toString (): ' 0 ' + date.getseconds ()); Str=str.replace (/s| S/g,date.getseconds ()); return str; }

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.