List the number of days in current date/month [duplicate] - date

This question already has answers here:
Get last day in month of time.Time
(4 answers)
Closed 5 months ago.
How to list the number of the days in the current date/month? Exp: January have 31 day so 1 2 3 4 till 31

Keep things simple.
The Go time package normalizes dates:
The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.
For the number of days in a month
func DaysInMonth(t time.Time) int {
y, m, _ := t.Date()
return time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}
For a list of days in a month
func ListDaysInMonth(t time.Time) []int {
days := make([]int, DaysInMonth(t))
for i := range days {
days[i] = i + 1
}
return days
}

The days in a month always range from 1 to the number of days in the given month. So the main task is to determine the number of days in a given month.
The time package does not expose such functionality, but you may use the following trick:
// Max days in year y1, month M1
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
daysInMonth := 32 - t.Day()
The logic behind this is that the day 32 is bigger than the max day in any month. It will get automatically normalized (extra days rolled to the next month and day decremented properly). And when we subtract day we have after normalization from 32, we get exactly what the last day was in the month.
This snippet is taken from the answer time.Since() with months and years.
So here's a little helper that returns the days of a month as []int for a given time.Time:
func daysInMonth(t time.Time) []int {
t = time.Date(t.Year(), t.Month(), 32, 0, 0, 0, 0, time.UTC)
daysInMonth := 32 - t.Day()
days := make([]int, daysInMonth)
for i := range days {
days[i] = i + 1
}
return days
}
Testing it:
fmt.Println(daysInMonth(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)))
fmt.Println(daysInMonth(time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC)))
fmt.Println(daysInMonth(time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC)))
Output (try it on the Go Playground):
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31]
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28]
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]
Another, less peformant option is to roll back the date to the first of the month, and start adding days until the month changes. This is how it could look like:
func daysInMonth(t time.Time) []int {
var days []int
// Roll back to day 1
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC)
m := t.Month()
for t.Month() == m {
days = append(days, t.Day())
t = t.AddDate(0, 0, 1)
}
return days
}
This will output the same. Try this one on the Go Playground.
Since all months contain at least 28 days, we can optimize the above solution to roll to day 29, and start checking and incrementing from there:
func daysInMonth(t time.Time) []int {
days := make([]int, 28, 31)
for i := range days {
days[i] = i + 1
}
m := t.Month()
// Roll to day 29
t = time.Date(t.Year(), t.Month(), 29, 0, 0, 0, 0, time.UTC)
for t.Month() == m {
days = append(days, t.Day())
t = t.AddDate(0, 0, 1)
}
return days
}
Try this one on the Go Playground.

There are many ways to get it done, as we are dealing with constants mostly, and we just need to handle February if it's a Leap Year( 29 days).
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(list_of_days())
}
func list_of_days() []int {
non_leap_year := map[string]int{
"January": 31,
"February": 28,
"March": 31,
"April": 30,
"May": 31,
"June": 30,
"July": 31,
"August": 31,
"September": 30,
"October": 31,
"November": 30,
"December": 31,
}
leap_year := map[string]int{
"January": 31,
"February": 29,
"March": 31,
"April": 30,
"May": 31,
"June": 30,
"July": 31,
"August": 31,
"September": 30,
"October": 31,
"November": 30,
"December": 31,
}
//get the current month
year, month, _ := time.Now().Date()
//handle leap year
no_of_days := 0
if year%4 == 0 && year%100 != 0 || year%400 == 0 {
no_of_days = leap_year[month.String()]
} else {
no_of_days = non_leap_year[month.String()]
}
days := make([]int, no_of_days)
for i := range days {
days[i] = i + 1
}
return days
}

Related

Flutter/Dart-Convert seconds to months, days and hours using simple_moment

Assuming I have a certain amount of seconds (eg:13456789456),How can I convert this to months,days and hours using the simple_moment package in flutter.
Try with this package.
import 'package:duration/duration.dart';
import 'package:duration/locale.dart';
void main() {
// => 5 days 9 hours
printDuration(aDay * 5 + anHour * 9);
// More examples
final dur = Duration(
days: 5,
hours: 23,
minutes: 59,
seconds: 59,
milliseconds: 999,
microseconds: 999);
// => 5 days 23 hours 59 minutes 59 seconds
printDuration(dur);
// => 3.455 milliseconds
printMilliseconds(aMicrosecond * 3455);
// => 3 seconds
printDuration(aMillisecond * 3000);
// => 2 seconds
printDuration(aMillisecond * 2250);
// => 1 day 3 hours 2 minutes
printDuration(aMillisecond * 97320000);
// => 5 días 9 horas
printDuration(aDay * 5 + anHour * 9,
abbreviated: false, locale: spanishLocale);
// => 5d, 23h, 59m, 59s, 999ms, 999us
printDuration(dur, abbreviated: true, tersity: DurationTersity.all);
// => 5 whole days 9 whole hours
printDuration(aDay * 5 + anHour * 9, spacer: ' whole ');
// => 5 days, 9 hours, 10 minute
printDuration(aDay * 5 + anHour * 9 + aMinute * 10, delimiter: ', ');
// => 5 days, 9 hours and 10 minutes
printDuration(aDay * 5 + anHour * 9 + aMinute * 10,
delimiter: ', ', conjugation: ' and ');
}

Get week of year (the first day of week is Sunday) could not return week 53

use strict;
use Time::Local;
use POSIX qw(strftime);
my $date = '12/31/1899';
my ($month, $day, $year) = split '/', $date;
my $epoch = timelocal( 0, 0, 0, $day, $month - 1, $year - 1900 );
my $week = strftime( "%U", localtime( $epoch ) );
printf "Date: %s Week: %s\n", $date, $week;
=> Date: 12/31/1899 Week: 53
However when $date = '12/30/1900', it return week 52 not week 53.
Could you please help me to point out the problem? Thanks.
%U
The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01. See also %V and %W.
By that definition, 1900-12-30 is part of week 52.
You can verify this yourself using a calendar of 1900.
Week 0 of 1900: Mon, Jan 1, 1900 to Sat, Jan 6, 1900
Week 1 of 1900: Sun, Jan 7, 1900 to Sat, Jan 13, 1900
...
Week 51 of 1900: Sun, Dec 23, 1900 to Sat, Dec 29, 1900
Week 52 of 1900: Sun, Dec 30, 1900 to Mon, Dec 31, 1900
That said, it's a fluke that you are getting the right answer (52). You are passing 0 ($year - 1900) as the last argument to timelocal, and that refers to the year 2000, not the year 1900.
This explains why you were getting 53 for 12/31/1900. While 1900-12-31 is in week 52, 2000-12-31 starts week 53.
To fix this, replace
timelocal( 0, 0, 0, $day, $month - 1, $year - 1900 )
with
timelocal( 0, 0, 0, $day, $month - 1, $year )

Tableau: sum of sales falls in set date

would like to seek your expertise on this calculation.
sum of sales should automatically fall in ND dates if queue date and ND is matched, workbook attached for reference here: https://community.tableau.com/thread/268194
ND
April 1, 2018 = 5617
April 2, 2018 = 0
April 3, 2018 = 0
April 4, 2018 = 0
April 5, 2018 = 0
April 6, 2018 = 0
April 7, 2018 = 0
April 8, 2018 = 0
April 9, 2018 = 4318
April 10, 2018 = 0
April 11, 2018 = 0
April 12, 2018 = 0
April 13, 2018 = 0
April 14, 2018 = 0
April 15, 2018 = 0
April 16, 2018 = 0
April 17, 2018 = 0
April 18, 2018 = 0
Thanks in advance :)

How does [hist] from SMLib work in Pure Data?

I put the following message into a [hist 0 100 10] object (in SMLib):
0 1 2 3 3 4 5 5 5 6 7 7 7 7 8 9 10 11 11 11 11 11 12 13 14 15 16 17 18 19 20 21 22 23 23 23 23 23 23 23 23 23 23 67 99 100 107
I then hit 'absolute' and the following is output.
6 19 18 0 0 0 0 1 0 3
I was expecting it to count the occurrences of the numbers into even bins of size 10 but only six numbers are in the first bin, and the 67 is in the wrong bin!
I counted up how it's evaluated it and got the following:
[0, 1, 2, 3, 3, 4] = 6
[5, 5, 5, 6, 7, 7, 7, 7, 8, 9, 10, 11, 11, 11, 11, 11, 12, 13, 14] = 19
[15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23] = 18
[] = 0
[] = 0
[] = 0
[] = 0
[67] = 1
[] = 0
[99, 100, 107] = 3
But.. I was expecting the following result.
16 14 13 0 0 0 1 0 0 3
Fixed it!
I was using [hist 0 100 10] when I should have been using [hist 5 105 10]!

How to calculate the day of the week based on unix time

I know that there are functions/classes in most programming languages to do that, but I would like to know the calculation.
So: How do I get from the unix time in seconds to a day-number (e.g. 0 for Sunday, 1 for Monday etc.)?
Thanks in advance. BTW: this is my first post on Stack Overflow.
The problem you ask is reasonably easy, compared to how ridiculously complicated other date/time functions can be (e.g. Zeller's congruence).
Unix time is defined as the number of seconds elapsed after January 1, 1970, at 00:00 (midnight) UTC.
You can look up a calendar to find that 1970-01-01 was a Thursday. There are 24 * 60 * 60 = 86400 seconds in a day.
Therefore values 0 to 86399 are Thursday, 86400 to 172799 are Friday, 172800 to 259199 are Saturday, etc. These are blocks of 86400 seconds aligned at 0.
Suppose T is your Unix timestamp. Then floor(T / 86400) tells you the number of days after 1970-01-01. 0 = Thursday January 1st; 1 = Friday January 2nd; 2 = Saturday January 3rd; etc.
Add 4 and modulo 7. Now 0 → 4; 1 → 5; 2 → 6; 3 → 0; 4 → 1; 5 → 2; 6 → 3; 7 → 4; 8 → 5; 9 → 6; 10 → 0; etc. This is your final answer.
In summary: day of week = (floor(T / 86400) + 4) mod 7.
(This assumes that you want the day of week in UTC. If you want to calculate it for another time zone, you need to perform some addition or subtraction of hours and minutes on T first.)
In JavaScript, days of the week are:
0 = Sun
1 = Mon
2 = Tue
3 = Wed
4 = Thu
5 = Fri
6 = Sat
You can use built-in methods:
// Unix epoch, 4 = Thu
new Date(0).getUTCDay()
// Today, 2 = Tue
new Date().getUTCDay()
Or a custom solution (remember to divide getTime() milliseconds by 1000):
// Unix epoch, 4 = Thu
(Math.floor(new Date(0).getTime() / 86400 / 1000) + 4) % 7
// Today, 2 = Tue
(Math.floor(new Date().getTime() / 86400 / 1000) + 4) % 7
Solution (from Geek for Geeks):
function dayOfWeek(d, m, y) {
let t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
y -= (m < 3) ? 1 : 0;
return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
// Unix epoch, 4 = Thu
Math.floor(dayOfWeek(1, 1, 1970))
// Today, 2 = Tue
Math.floor(dayOfWeek(7, 12, 2021))
https://www.geeksforgeeks.org/find-day-of-the-week-for-a-given-date/