time & last day of month

2,778 views
Skip to first unread message

gwenn

unread,
Oct 31, 2012, 3:27:37 PM10/31/12
to golang-nuts
Hi,
Could you please help me find the way to compute the last day of month?
While looking into time package, I've found 'daysIn' but it is not exported...
Thanks.

Note: In Java, the last day of month can be computed this way: Calendar.getActualMaximum(DAY_OF_MONTH) (standard lib), LocalDate.dayOfMonth().getMaximumValue() (joda time).

Ian Lance Taylor

unread,
Oct 31, 2012, 3:50:41 PM10/31/12
to gwenn, golang-nuts
On Wed, Oct 31, 2012 at 12:27 PM, gwenn <gwenn...@gmail.com> wrote:
> Hi,
> Could you please help me find the way to compute the last day of month?
> While looking into time package, I've found 'daysIn' but it is not
> exported...

The last day of the month depends on the year, of course.

Here is one way to do it.

http://play.golang.org/p/kTgmzTkyJs

Ian

Rémy Oudompheng

unread,
Oct 31, 2012, 3:53:21 PM10/31/12
to Ian Lance Taylor, gwenn, golang-nuts
Another way of doing:
http://play.golang.org/p/xkSxRtckGh

Rémy.

Kyle Lemons

unread,
Oct 31, 2012, 6:13:37 PM10/31/12
to Rémy Oudompheng, Ian Lance Taylor, gwenn, golang-nuts
A day doesn't always have 24 hours.



Rémy.

--



Rémy Oudompheng

unread,
Oct 31, 2012, 6:16:20 PM10/31/12
to Kyle Lemons, Ian Lance Taylor, gwenn, golang-nuts
On 2012/10/31 Kyle Lemons <kev...@google.com> wrote:
> A day doesn't always have 24 hours.

The Go days have no leap seconds and the example was in UTC location.
Isn't that enough ?

Rémy.

jonath...@gmail.com

unread,
Apr 10, 2014, 1:27:22 AM4/10/14
to golan...@googlegroups.com, Kyle Lemons, Ian Lance Taylor, gwenn
Old thread, so just for posterity..

Maybe just use year, m+1, 0 instead of year, m+1, 1 so you can avoid subtracting a day using -24 hours...

nicolas...@gmail.com

unread,
Apr 10, 2014, 3:55:32 PM4/10/14
to golan...@googlegroups.com, gwenn...@gmail.com

Shawn Milochik

unread,
Apr 10, 2014, 4:41:16 PM4/10/14
to golan...@googlegroups.com
This way seems simplest to me.


pa...@daishisystems.com

unread,
Oct 6, 2015, 8:39:23 AM10/6/15
to golang-nuts, gwenn...@gmail.com
This library contains functionality designed to return the last day of any given month:

messju mohr

unread,
Oct 6, 2015, 8:52:48 AM10/6/15
to golang-nuts
Another library woule be jinzhu's Now: <https://github.com/jinzhu/now>

now.EndOfMonth().Day() => 31
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [1]golang-nuts...@googlegroups.com.
> For more options, visit [2]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. mailto:golang-nuts...@googlegroups.com
> 2. https://groups.google.com/d/optout

gwenn

unread,
Oct 6, 2015, 11:45:15 AM10/6/15
to pa...@daishisystems.com, golang-nuts
Sorry but I don't understand the following part: "that is not
available in standard Go libraries."
The link
http://play.golang.org/p/ixFz2w8ozR
previously posted in this thread proves the opposite, no ?

pa...@daishisystems.com

unread,
Oct 6, 2015, 12:15:48 PM10/6/15
to golang-nuts, pa...@daishisystems.com, gwenn...@gmail.com

Thank you for pointing this out. I hadn’t considered that solution when drafting the package. Yes, I agree; the functionality is present in the core libraries. I’ve modified the package blurb accordingly.

Reply all
Reply to author
Forward
0 new messages