How can I parse timespan string to hour, minutes?
问题 I have a string "P18DT5H2M3S" which means: 18 days, 5 hours, 2 minutes, 3 seconds. I have to parse this string to hour and minute. Should I use regex or split or substr etc...? (regarding to this How can I convert come string timespan variable from Wcf to hours and minutes?) 回答1: You can use whatever you want. Following is the example using split method with regex. var res = "P18DT5H2M3S"; var tokens = res.split(/[A-Z]+/); //var str = "D:"+token[1]+" H:"+tokens[2]+" M:"+tokens[3]+" S:"+tokens