D3.js - get number of days for a certain month - date

I'm using D3.js (v3) to plot a time-series graph and I'm trying to figure out the amount of ticks I need for a given month (ie, days). From what I can make of the documentation, d3.time-month should return something from 28 to 31 days, which is what I want, but for some reason I'm obviously missing, I can't seem to get the number I need.
Can anyone help?
This is what I've got so far:
console.log(date); // Fri Jul 01 2016 00:00:00 GMT+0100 (WEST)
monthDays = d3.time.month(date);
console.log(currentMonth+" has "+monthDays+" days."); // July has Fri Jul 01 2016 00:00:00 GMT+0100 (WEST) days.

You're misreading the documentation on intervals : the number of days in the intervals used by d3.time.month have a length between 28 and 31 but the functions are defined as
interval(date)
Alias for interval.floor(date). [...]
and
interval.floor(date)
Rounds down the specified date, returning the latest time interval
before or equal to date. [...]
Basically, d3.time.month(date) will return the first day of the month at midnight, not the number of days in that month.
How to get the number of days then? As far as I can tell, D3 does not expose a way to get the length of the month for a given date. You could of course get the range of days for a given month and extract its length:
var date = new Date(2016, 01, 02); // 2016-02-02
console.log(
d3.time.days(d3.time.month(date), d3.time.month.ceil(date)).length
)
or probably more efficiently use plain JS like in this answer https://stackoverflow.com/a/1185804/1071630 :
var date = new Date(2016, 01, 02); // 2016-02-02
console.log(
new Date(date.getFullYear(), date.getMonth()+1, 0).getDate()
)
var date = new Date(2016, 01, 02);
console.log(
d3.time.days(d3.time.month(date), d3.time.month.ceil(date)).length
)
console.log(
new Date(date.getFullYear(), date.getMonth()+1, 0).getDate()
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

Related

How to get dates by days which have intervals of 7 days each

hello guyz am new to flutter and am practicing on dates then i encounter a difficulty on how can i acheive the result same below using a loop or something in order to get this result.
so far i tried is
final now = DateTime.now();
for(var d = 1 ; d <= 5 ; d++){
// Don't know whats next to do here to get the same result below
}
i want result like this
Apr 19, 2022
Apr 26, 2022
May 3, 2022
May 10, 2022
May 17, 2022
can some help me and explain.
Dart's DateTime class has an add function, where you can specify a duration which will be added the current Date.
Therefore, what you can do is:
DateTime now = DateTime.now();
for(var d = 1; d <= 5; d++) {
...
now = now.add(Duration(days: 7)); // here you can specify your interval. Also possible: hours, minutes, seconds, milliseconds and microseconds
}
I'll recommend reading the Docs, e.g. DateTime Docs and Duration Docs

print first monday of every week of current month Flutter/Dart

I have a calendar in my Flutter App and I need to print a list of the weeks in the current month. But rather than starting on the 1st day of each month, it needs to start with the first Monday of the month (e.g. 05 April 2021 as the first Monday of April 2021). Then I need to print out the following weeks in that month, again starting from Monday. This includes the days of the next month that the final week of the current month follows on from (e.g. 26 April 2021 - 02 May 2021). It should print like this:
05 Apr - 11 Apr
12 Apr - 18 Apr
19 Apr - 25 Apr
26 Apr - 02 May
Start by figuring out how to find how many days there are from a given weekday to the Monday on or after that day. Examples help; if the given weekday is:
Monday, add 0 days.
Tuesday, add 6 days.
Wednesday, add 5 days.
... etc. ...
Sunday, add 1 day.
We could make a lookup table, but we also can devise that the offset (in days) from a given weekday to the Monday on or after that day has the form (7 - x) % 7
where x corresponds to the given weekday. We'd want that value to be 0 for Monday, 1 for Tuesday, and so on, until 6 for Sunday. Dart's DateTime.weekday uses values 1 (DateTime.monday) through 7 (DateTime.sunday), so we can easily map that to the value we want via DateTime.weekday - DateTime.monday.
Once we compute that offset, we can find the first day of the current month, add that offset to find the first Monday of the month, and then you can iteratively add 7 days until you reach the next month, and we can use DateFormat from package:intl to format the dates the way you want:
import 'package:intl/intl.dart';
String formatDate(DateTime dateTime) => DateFormat('dd MMM').format(dateTime);
void main() {
var now = DateTime.now();
var firstOfMonth = DateTime(now.year, now.month, 1);
var firstMonday =
firstOfMonth.addCalendarDays((7 - (firstOfMonth.weekday - DateTime.monday)) % 7);
var currentMonday = firstMonday;
while (currentMonday.month == now.month) {
var nextMonday = currentMonday.addCalendarDays(7);
var nextSunday = nextMonday.addCalendarDays(-1);
print('${formatDate(currentMonday)} - ${formatDate(nextSunday)}');
currentMonday = nextMonday;
}
}
See https://stackoverflow.com/a/68216029/ for the implementation of the addCalendarDays extension method.

how can i get the day of the week for a particular given date in Matlab

As today is Wednesday with date June 8, 2016. how can i write a code to get the day of given dates:
like what day is Nov 29
I'm trying to create a struct with
date
day
month
with month and date as input
Use the builtin weekday() function:
>> [num, name] = weekday('08-Jun-2016')
num =
4
name =
Wed
>> [num, name] = weekday('29-Nov-2016')
num =
3
name =
Tue
In addition to the weekday function, you can use the DDD or DDDD formats in the datestr function, like this:
datestr('08-Jun-2016','DDD') %Returns the string 'Wed'
datestr('08-Jun-2016','DDDD') %Returns the string 'Wednesday'
Or, to use a more practical format
datestr('08-Jun-2016','DDDD, mmmm DD, yyyy')
% Returns the string: 'Wednesday, June 08, 2016'

Get current hour with Protractor

I need to test an agenda and I need getting the actual date and current hour to assert with the agenda values.
This not works:
var d = new Date();
expect(day.getText()).toEqual(d.getDate());
You can get current hour using JavaScript inbuilt getHours() method. Here's its usage -
var cTime = Date().getHours();
or
var d = new Date();
var cTime = d.getHours(); //prints the current hour only
And you can get the actual date using getDate() method which prints a string of values related to current date and time. Sample format: Fri Nov 20 2015 20:24:38 GMT+0530 (IST). Hope it helps

Converting string to date in ExtJs

I have a date in string format as follows-
"Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)"
I want to check whether the date is a month old or not.
For this I am doing like this-
var todaysDate= new Date();
todaysDate.setDate(todaysDate.getDate() - 30);
if(Ext.util.Format.dateRenderer("Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)", "D M d Y g:i:s") <= todaysDate)
{
}
It should return true but it is returning false. What I am doing wrong here.
Please help.
You're comparing a renderer to a date, dateRenderer doesn't return a date, it returns a renderer. Renderers are used for example for grid cells, when you have a grid that has a record with a date and you want to display that date in a particular format you use a renderer to tell the grid cell how to format that date.
Check the docs about this: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.util.Format-method-dateRenderer
Also I believe dateRenderer takes only one argument, you're passing two.
You can use regular Javascript for this:
var oldDate = new Date("Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)");
var newDate = new Date()
newDate.setDate(newDate.getDate() - 30);
if(oldDate <= newDate){
doSomething()
}