Share a JS function that converts a string of the specified date format to a date todate

Source: Internet
Author: User

Today found Easyui Datetimebox can not display the normal date, and then nature is to realize the function of ToDate, Google, did not find satisfaction, are written very wordy. Write your own, originally thought JS can also be used to name group, but the current look at JavaScript does not seem to support. Now implement the code below and hope to be helpful to those who need it.


by Dragonimp 2015.5.3//use://"2015-5-5 9:10:10". ToDate ("yyyy-m-d hh:mm:ss")//"2015/5/5 9:10:10". ToDate ("yyyy/m/d Hh:mm:ss ") String.prototype.toDate = function (FMT) {    if (FMT = = null) FMT = ' Yyyy-mm-dd hh:mm:ss ';    var str = this;    FMT is a date format, str is a date string    var reg =/([ymdhmss]+)/g;//the characters in the date format    var key = {};    var Tmpkeys = Fmt.match (reg);    for (var i = 0; i < tmpkeys.length; i++) {        key[tmpkeys[i].substr (0, 1)] = i + 1;    }    var r = Str.match (Fmt.replace (Reg, "(\\d+)"));    return new Date (r[key["y"], r[key["M"]]-1, r[key["D"]], r[key["H"]], r[key["M"]], r[key["s"]], r[key["s"]);}

Attached: Easyui problems and solutions.

Problem:

<input type= "text" value= "2015/5/6 23:00:00" id= "xxxx" class= "Easyui-datetimebox"/>
this displayed control, the date "Day" section is displayed incorrectly, and the date of the day is displayed.

Check the following, Easyui has quoted the Chinese resource file, looked under the resource file, should only support "-" split the date, I am here "/"

Solution: You need to implement formatter and parser yourself.

1. Add Options

<input type= "text" value= "2015/5/6 23:00:00" id= "xxxx" class= "Easyui-datetimebox" data-options= "Formatter: Datetimebox_format,parser:datetimebox_parse "/>

2. Implementation of parsing and formatting functions

function Datetimebox_format (date) {return date.toformatstring ("yyyy/m/d h:mm:ss");}  function Datetimebox_parse (s) {return s.todate ("yyyy/m/d hh:mm:ss"); }//a) Turn the date, see preceding my todate//b) to the string below, from the online Date.prototype.toFormatString = function (fmt) {var o = {"m+": This.get                   Month () + 1,//month "d+": this.getdate (),//day "H +": this.gethours (), Hours "m+": this.getminutes (),//min "s+": This.getseconds (),/    /sec "q+": Math.floor ((This.getmonth () + 3)/3),//Quarterly "S": this.getmilliseconds ()//msec};    if (/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));  For (var k in O) if (New RegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)?    (O[k]): (("XX" + o[k]). substr (("" + O[k]).)); return FMT;}


Share a JS function that converts a string of the specified date format to a date todate

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.