javascript - Match a pattern with a string and extract information -
i working on google apps script. want user enter pattern mm/dd/yyyy hh:mm:ss
according pattern want extract information string. string 02/29/2016 07:00:00 pm est
. want extract date, month, year, hour, minute , second time-stamp string using pattern given user.
how can achieved in javascript ?
you can try example (in google apps script):
var = extractdatetimeinfo('mm/dd/yyyy hh:mm:ss', '02/29/2016 07:10:14 pm est'); logger.log(a); //output: year: 2016, month: 02, date: 29, hours: 07, min: 10, sec: 14 var b = extractdatetimeinfo('yyyy-mm-dd hh:mm', '2016-12-26 16:13 pm est'); logger.log(b); //output: year: 2016, month: 12, date: 26, hours: 16, min: 13, sec: function extractdatetimeinfo(patt, t) { var yyyypos = patt.indexof('yyyy'); var mmpos = patt.indexof('mm'); var ddpos = patt.indexof('dd'); var hhpos = patt.indexof('hh'); var mmpos = patt.indexof('mm'); var sspos = patt.indexof('ss'); var yyyy = yyyypos >= 0 ? t.substr(yyyypos, 4) : ''; var mm = mmpos >= 0 ? t.substr(mmpos, 2) : ''; var dd = ddpos >= 0 ? t.substr(ddpos, 2) : ''; var hh = hhpos >= 0 ? t.substr(hhpos, 2) : ''; var mm = mmpos >= 0 ? t.substr(mmpos, 2) : ''; var ss = sspos >= 0 ? t.substr(sspos, 2) : ''; return utilities.formatstring("year: %s, month: %s, date: %s, hours: %s, min: %s, sec: %s", yyyy, mm, dd, hh, mm, ss); }
however don't know type of pattern given user expect. can adapt other possibilities yy etc.
you can put result array in extractdatetimeinfo function well...
Comments
Post a Comment