The default time.Duration String method formats the duration with adding 0s for minutes and 0m0s for hours. Is there function that I can use that will produce 5m instead 5m0s and 2h instead 2h0m0s ... or I have to implement my own?
Time duration to string 2h instead 2h0m0s
22.9k Views Asked by gsf At
2
There are 2 best solutions below
0
Tarion
On
You can just round the duration like this:
func RountTime(roundTo string, value time.Time) string {
since := time.Since(value)
if roundTo == "h" {
since -= since % time.Hour
}
if roundTo == "m" {
since -= since % time.Minute
}
if roundTo == "s" {
since -= since % time.Second
}
return since.String()
}
Related Questions in STRING
- SML - Find same elements in a string
- match hex string with list indice
- How can I determine the index of the same set of characters between two strings that are of different lengths?
- String.replace() isn't working like I expect
- How to do a case-insensitive string comparison?
- Trying to save an np array with string and floats, but getting a error
- String replace with integer not working
- How to calculate a length of array with out using library
- Java replace every Nth specific character (e.g. space) in String
- Split the strings into two parts Python
- Perl Regex: Merge multiple one-character substrings
- Squid S2275 does not know about format string argument indexes
- more efficient way of remove a few characters from the end of a string
- python member str performance too slow
- String.split() not behaving in android
Related Questions in GO
- How do I get all the attributes of an XML element using Go?
- Type cast custom types to base types
- Why are Revel optional func parameters in controller not working? CRUD code redundancy
- Streaming commands output progress
- single ampersand between 2 expressions
- golang goroutine use SSHAgent auth doesn't work well and throw some unexpect panic
- How do I do a literal *int64 in Go?
- Emulating `docker run` using the golang docker API
- How to print contents of channel without changing it
- Golang time zone parsing not returning the correct zone on ubuntu server
- Is os.File's Write() threadsafe?
- How to get the pointer of return value from function call?
- How do I represent an Optional String in Go?
- Fibonacci in Go using channels
- Boltdb-key-Value Data Store purely in Go
Related Questions in TIME
- Perform a task each interval
- PLSQL Need REFCURSOR DATE + TIME
- How to set timezone to the local in Rails?
- How to check duplicates in an array with a time complexity of O(n) in C lang?
- Having trouble writing a calendar, I couldn't find a detailed guide answering my questions anywhere
- How to write the current time to a new line of a .txt file on php execution
- Golang time zone parsing not returning the correct zone on ubuntu server
- CMD specifying columns to save?
- How do I check if a time is between two times which are stored as Strings in Ruby on Rails?
- having issues with python time object
- Why Google Analytics show bounce rate 100% and avg time more then 1min
- time formatting in matlab
- Python - creating a delay that takes in account the execution time
- JodaTime Comparing two dates with different time zone
- The simple timezone clock function falls into incorrect condition and I have no idea why
Related Questions in FORMATTING
- C++: Re-use line printed to console
- NSAttributeString - How to remove auto formatting for a date or time item?
- Bifurcate a string, removing the middle of the string instead of end
- Use DateTime format in a class but restrict time tokens
- Doesn't change the format of columns to date
- How do I convert a double into an n-character string using exponential notation?
- Java Integer Pyramid
- proper way to get nice string from exception
- How to edit text in columns?
- Simple and clean java float to string conversion
- Having trouble with list post-processing
- Formatting equations?
- Looking to export the content from a website into doc files
- SAPUI5 local scope object as formatter function parameter
- DataTable contains strings with Hashtags when imported from .xlsx instead of .xls
Related Questions in DURATION
- Asterisk: How to convert time to seconds
- how to convert javax.xml.datatype.Duration
- How to format duration in Python (timedelta)?
- Save duration with precision in Parse.com
- Time duration to string 2h instead 2h0m0s
- How to get the time duration from subtracting two times
- How to remove songs which has 0 duration in cursor in android
- Show hidden div on :hover, and make div stay visible for 5 seconds after mouseout on CSS / CSS3
- backstretch.js duration out of sync with .animate() delay
- Notification like Toast but with longer duration
- ISO 8601 Duration with milliseconds?
- How to use the Java 8 Duration.of(amount, unit) method?
- Windows Phone 8 - get duration of a Song object when use MediaPlayer
- What Java library object encapsulate a number and `TimeUnit`?
- How to check duration of video in Angular 4.3.1 using ng2-file-upload?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Foreword: I released this utility in
github.com/icza/gox, seetimex.ShortDuration().Not in the standard library, but it's really easy to create one:
Testing it:
Output (try it on the Go Playground):