Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to iterate over specific month or week

Tags:

date

go

Is there a way, in go, to iterate over a specific month and get all time.Date objects from it?

For instance iterate over April will result in 04012016 until 04312016:

for _, dayInMonth := range date.April {
   // do stuff with dates returned
}

(Currently the above code will not work obviously).

Or if not part of the standard library is there a third party library that equivalent to moment.js?

like image 373
Shikloshi Avatar asked Jun 07 '16 12:06

Shikloshi


People also ask

How do you iterate over months in Python?

Method 2: rrule rrule is a package present in dateutil library and this package consists of a method also rrule which takes dtstart, until and specific time period as parameters which are start date, end date, and time period based on iteration respectively. Specific time periods are WEEKLY, MONTHLY, YEARLY, etc.

How do pandas iterate over dates?

We can use the date_range() function method that is available in pandas. It is used to return a fixed frequency DatetimeIndex. We can iterate to get the date using date() function.

Can you iterate over set?

There is no way to iterate over a set without an iterator, apart from accessing the underlying structure that holds the data through reflection, and replicating the code provided by Set#iterator...


1 Answers

There is no time.Date object defined in the standard library. Only time.Time object. There's also no way to range loop them, but looping them manually is quite simple:

// set the starting date (in any way you wish)
start, err := time.Parse("2006-1-2", "2016-4-1")
// handle error

// set d to starting date and keep adding 1 day to it as long as month doesn't change
for d := start; d.Month() == start.Month(); d = d.AddDate(0, 0, 1) {
    // do stuff with d
}
like image 88
jussius Avatar answered Oct 05 '22 12:10

jussius