[英]How can I split a string given a specific format?
I have dates in string format that sometimes are like this: 05-11-2009 16:59:20
and sometime are like this: 2013-12-05T22:00:00:00Z
and some other time are like this: 2013-12-05T22:00:00:00.000Z
. 我有一些字符串格式的日期,有时像这样: 05-11-2009 16:59:20
有时是这样的: 2013-12-05T22:00:00:00Z
还有其他时间是这样的: 2013-12-05T22:00:00:00.000Z
。
I wrote a function that extract the month day and year from those dates, but I would like a function that can work for all of them passing in input the format. 我编写了一个从这些日期中提取月份和月份的函数,但是我希望该函数可以对所有以输入格式传递的日期起作用。 something like: 就像是:
function DateParts(datetime, format) {
var matches = datetime.splitByFormat(format);
this.getYear = function () {
return matches[3];
};
this.getMonth = function () {
return (matches[2] - 1) +"" ;
};
this.getDay = function () {
return matches[1];
};
this.getHours = function () {
return matches[4];
};
this.getMinutes = function () {
return matches[5];
};
this.getSeconds = function () {
return matches[6];
};
};
Where format will be soething like "yyyy-mm-dd hh:MM:ss"
or "dd-mm-yyyyThh:MM:ss.dddZ"
or whatever. 格式应该像"yyyy-mm-dd hh:MM:ss"
或"dd-mm-yyyyThh:MM:ss.dddZ"
。
Is there a nice way of creating the splitByFormat function without having to substring my head off? 有没有一种很好的方法来创建splitByFormat函数,而不必再烦恼了吗?
One regex to find them all: 一个正则表达式可以找到它们全部:
(((?<month>\d{2})-(?<day>\d{2})-(?<year>\d{4})\s(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2}))|((?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2}):(?<millisecond>\d{2})Z)|(?<year>(\d{4})-(?<month>\d{2})-(?<day>\d{2})T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2}):(?<millisecond>\d{2})\.(?<centisecond>\d{3})Z))
If you use this regex string, you are able to just capture the different groupings: 如果使用此正则表达式字符串,则可以捕获不同的分组:
What about this? 那这个呢? anything better anyone? 有什么更好的人吗?
function DateParts(datetime, format) {
this.getYear = function () {
if (format) {
ind = format.indexOf("yyyy");
return datetime.substring(ind, ind + 4);
}
return "";
};
this.getMonth = function () {
if (format) {
ind = format.indexOf("mm");
return datetime.substring(ind, ind + 2);
}
return "";
};
this.getDay = function () {
if (format) {
ind = format.indexOf("gg");
return datetime.substring(ind, ind + 2);
}
return "";
};
};
This simple class FormatAnalyzer
could be a beginning. 这个简单的类FormatAnalyzer
可能是一个开始。 It works. 有用。
FormatAnalyzer f = new FormatAnalyzer("yyyy-mm-dd hh:MM:ss");
f.getYear("2013-07-11 15:39:00");
f.getMonth("2013-07-11 15:39:00");
where 哪里
public class FormatAnalyzer {
private String format;
private int yearBegin;
private int yearEnd;
private int monthBegin;
private int monthEnd;
// ...
public FormatAnalyzer(String format) {
this.format = format;
analyzeFormat();
}
public String getYear(String date) {
return date.substring(yearBegin, yearEnd);
}
public String getMonth(String date) {
return date.substring(monthBegin, monthEnd);
}
private void analyzeFormat() {
yearBegin = yearEnd = format.indexOf("y");
while (format.indexOf("y", ++yearEnd) != -1) {
}
monthBegin = monthEnd = format.indexOf("m");
while (format.indexOf("m", ++monthEnd) != -1) {
}
// and so on for year, day, ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.