C
C#3y ago
CTJ

TimeSpan formatter [Answered]

Hi, is there any function or library that would take an time length input, for example 5 min or 3 days and convert that to TimeSpan or other time durationm object? kinda like js library in screenshot bellow
13 Replies
Angius
Angius3y ago
new TimeSpan(3, 0, 5, 0) days, hours, minutes, seconds
Doombox
Doombox3y ago
https://github.com/ti-ka/human-date-parser there's this (quite old) library which does something like that, https://github.com/microsoft/Recognizers-Text/ there's also this which seems to possibly do something similar, https://www.nuget.org/packages/Microsoft.Recognizers.Text.DateTime/ there is a discrete package just for that bit other than that, parsing "human" input is a whole load of work, there are pretty simple naive approaches but they break down kinda fast
CTJ
CTJOP3y ago
Wow these look good according to github page
Doombox
Doombox3y ago
depending on what the scope of your input is though a naive parser of your own would be fine
CTJ
CTJOP3y ago
Well, it will be used with discord bot to specify duration of ceartain action abnd I want to make it as easy as possible to use
Doombox
Doombox3y ago
actual human input is always a coin flip, I'd probably try one of these libraries and add enough user feedback so that people can figure it out
CTJ
CTJOP3y ago
i saw that you can just poarse TImeSpan in form of 3:0:5:0 but I dont know if users will like this form Yee thats the plan for now
Doombox
Doombox3y ago
I mean you could also accept !someCommand d:1 h:2 m:3 and then you can make a really simple parser
CTJ
CTJOP3y ago
That could go too Alright, ill try microsoft parser first and see how it turns out if it fails I can alwys go to simpler solution Thanks for help!
Doombox
Doombox3y ago
no worries, /close the thread when you're happy it's solved sunglas
CTJ
CTJOP3y ago
Alright!
Accord
Accord3y ago
✅ This post has been marked as answered!
CTJ
CTJOP3y ago
Epic
Want results from more Discord servers?
Add your server