I am working on a Google Apps Script. I want the user to enter a pattern like MM/DD/YYYY hh:mm:ss
我正在开发Google Apps脚本。我希望用户输入类似MM / DD / YYYY hh:mm:ss的模式
According to the pattern I want to extract information from a string. The string is like 02/29/2016 07:00:00 PM EST
. I want to extract date, month, year, hour, minute and second from this time-stamp string using the pattern given by the user.
根据模式,我想从字符串中提取信息。该字符串就像美国东部时间02/29/2016 07:00:00。我想使用用户给出的模式从此时间戳字符串中提取日期,月份,年份,小时,分钟和秒。
How this can be achieved in JavaScript ?
如何在JavaScript中实现这一目标?
3 个解决方案
#1
1
You can try this example (in Google apps script):
您可以尝试此示例(在Google应用脚本中):
var a = 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 I don't know what type of pattern given by the user you expect. But you can adapt it for other possibilities like YY etc.
但是,我不知道您期望的用户给出的模式类型。但你可以适应其他可能性,如YY等。
You can put the result into array in extractDateTimeInfo function as well...
您可以将结果放入extractDateTimeInfo函数中的数组中......
#2
0
This regex will work(but does not check whether the value entered by user is valid or not)
此正则表达式将起作用(但不检查用户输入的值是否有效)
(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)
正则表达式演示
JS Code
JS代码
var re = /(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/;
var str = '02/29/2016 07:00:00 PM EST';
var result = str.match(re);
document.writeln(result[1] + '<br>');
document.writeln(result[2] + '<br>');
document.writeln(result[3] + '<br>');
document.writeln(result[4] + '<br>');
document.writeln(result[5] + '<br>');
document.writeln(result[6] + '<br>');
Ideone演示
#3
0
Try this :
尝试这个 :
/^(0?\d|1[0-2])\/([0-2]?\d|3[01])\/(\d{1,4})\s([01]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)$/
I restricted the input the most i can do :
Group 1 : Months, restricted from 0 to 12
Group 2 : Days, restricted from 0 to 31
Group 3 : Years, restricted to 4 characters
我限制输入我能做的最多:组1:月份,限制为0到12组2:天,限制为0到31组3:年,限制为4个字符
Group 4 : Hours, restricted from 0 to 23
Group 5 : Minutes, restricted from 0 to 59
Group 6 : Seconds, restricted from 0 to 59
第4组:小时数,限制在0至23组5:分钟,限制在0至59组6:秒,限制在0至59之间
#1
1
You can try this example (in Google apps script):
您可以尝试此示例(在Google应用脚本中):
var a = 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 I don't know what type of pattern given by the user you expect. But you can adapt it for other possibilities like YY etc.
但是,我不知道您期望的用户给出的模式类型。但你可以适应其他可能性,如YY等。
You can put the result into array in extractDateTimeInfo function as well...
您可以将结果放入extractDateTimeInfo函数中的数组中......
#2
0
This regex will work(but does not check whether the value entered by user is valid or not)
此正则表达式将起作用(但不检查用户输入的值是否有效)
(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)
正则表达式演示
JS Code
JS代码
var re = /(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/;
var str = '02/29/2016 07:00:00 PM EST';
var result = str.match(re);
document.writeln(result[1] + '<br>');
document.writeln(result[2] + '<br>');
document.writeln(result[3] + '<br>');
document.writeln(result[4] + '<br>');
document.writeln(result[5] + '<br>');
document.writeln(result[6] + '<br>');
Ideone演示
#3
0
Try this :
尝试这个 :
/^(0?\d|1[0-2])\/([0-2]?\d|3[01])\/(\d{1,4})\s([01]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)$/
I restricted the input the most i can do :
Group 1 : Months, restricted from 0 to 12
Group 2 : Days, restricted from 0 to 31
Group 3 : Years, restricted to 4 characters
我限制输入我能做的最多:组1:月份,限制为0到12组2:天,限制为0到31组3:年,限制为4个字符
Group 4 : Hours, restricted from 0 to 23
Group 5 : Minutes, restricted from 0 to 59
Group 6 : Seconds, restricted from 0 to 59
第4组:小时数,限制在0至23组5:分钟,限制在0至59组6:秒,限制在0至59之间