Unlike C #, Java, a high-level language, has rich syntax sugar for developers to call conveniently. Therefore, this has spawned a lot of open source components. The use of these third-party components can help us step on a lot less holes in the development process.
Time processing is a problem faced by all languages. parse converts the string to date type, and tostring() converts the date type to customized string.
In the actual use process, there is an uncomfortable way to use parse.
Upper source code
time1, _ := time.Parse("2006-01-02", "2020-02-22") fmt.Println(time1) time2, _ := time.Parse("2006/01/02", "2020/02/23") fmt.Println(time2)
Different string formats need to be configured with different templates before they can be parsed normally. Is there a way similar to datetime in C# Parse ("date string"), generally the input format can be recognized.
t, _ := now.Parse("2017/01/02") fmt.Println(t) t2, _ := now.Parse("2017-10-02 17:30") fmt.Println(t2)
It's very convenient. You don't have to remember the strange date format of 2006-01-02 15:04:05. I really don't understand. Nowadays, yyyy MM DD is rampant, why the author should design such a strange date formatting method is beyond wonder.
This is just a function of the now toolbox, and there are many other additional functions waiting for you to explore.
Basic use
import "github.com/jinzhu/now" time.Now() // 2013-11-18 17:51:49.123456789 Mon now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon now.BeginningOfHour() // 2013-11-18 17:00:00 Mon now.BeginningOfDay() // 2013-11-18 00:00:00 Mon now.BeginningOfWeek() // 2013-11-17 00:00:00 Sun now.BeginningOfMonth() // 2013-11-01 00:00:00 Fri now.BeginningOfQuarter() // 2013-10-01 00:00:00 Tue now.BeginningOfYear() // 2013-01-01 00:00:00 Tue
Set the starting days of each week
It is customary to use Sunday as the first day of the week in foreign countries, while Monday is used as the first day of the week in China. The setting of now is very simple, just one line of code
now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday now.BeginningOfWeek() // 2013-11-18 00:00:00 Mon
At this time, every day of the week becomes the 18th
”Is there a simpler way? I don't want to configure it every time. I may forget it sometimes“
"Of course."
now.Monday() // 2013-11-18 00:00:00 Mon now.Sunday() // 2013-11-24 00:00:00 Sun (Next Sunday) now.EndOfSunday() // 2013-11-24 23:59:59.999999999 Sun (End of next Sunday)
Direct Monday() .Sunday().
EndOfSunday() is obtained to the last second of Sunday. It is used when the query condition start date < a < end date.
Calculate time based on defined configuration
It is equivalent to defining some basic contents through a configuration information, such as input and output format, time zone, starting day parameters of the week. All subsequent methods called with this instance will be based on this configuration.
The following code is explained as follows:
Define the time string input / output format as "2006-01-02 15:04:05", take Monday as the first day of the week, and the time zone uses the local time zone of the system.
location, err := time.LoadLocation("Asia/Shanghai") myConfig := &now.Config{ WeekStartDay: time.Monday, TimeLocation: location, TimeFormats: []string{"2006-01-02 15:04:05"}, } t := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location()) // // 2013-11-18 17:51:49.123456789 Mon myConfig.With(t).BeginningOfWeek() // 2013-11-18 00:00:00 Mon myConfig.Parse("2002-10-12 22:14:01") // 2002-10-12 22:14:01 myConfig.Parse("2002-10-12 22:14") // returns error 'can't parse string as time: 2002-10-12 22:14'