Minimum and Maximum Date

Minimum and maximum date

From the spec, §15.9.1.1:

A Date object contains a Number indicating a particular instant in time to within a millisecond. Such a Number is called a time value. A time value may also be NaN, indicating that the Date object does not represent a specific instant of time.

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC. In time values leap seconds are ignored. It is assumed that there are exactly 86,400,000 milliseconds per day. ECMAScript Number values can represent all integers from –9,007,199,254,740,992 to 9,007,199,254,740,992; this range suffices to measure times to millisecond precision for any instant that is within approximately 285,616 years, either forward or backward, from 01 January, 1970 UTC.

The actual range of times supported by ECMAScript Date objects is slightly smaller: exactly –100,000,000 days to 100,000,000 days measured relative to midnight at the beginning of 01 January, 1970 UTC. This gives a range of 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC.

The exact moment of midnight at the beginning of 01 January, 1970 UTC is represented by the value +0.

The third paragraph being the most relevant. Based on that paragraph, we can get the precise earliest date per spec from new Date(-8640000000000000), which is Tuesday, April 20th, 271,821 BCE (BCE = Before Common Era, e.g., the year -271,821).

How to get the minimum and maximum date

Date does not have this

Actually, it does, but only indirectly. According to the specification, a Date object's milliseconds-since-the-Epoch value can only be in the range -8640000000000000 to 8640000000000000.

So the minimum date is new Date(-8640000000000000) (Tue, 20 Apr -271821 00:00:00 GMT), and the maximum date is new Date(8640000000000000) (Sat, 13 Sep 275760 00:00:00 GMT).

If you wanted, you could put those on the Date function as properties:

Date.MIN_VALUE = new Date(-8640000000000000);
Date.MAX_VALUE = new Date(8640000000000000);

...but since Date instances are mutable, I probably wouldn't, because it's too easy to accidentally modify one of them. An alternative would be to do this:

Object.defineProperties(Date, {
MIN_VALUE: {
value: -8640000000000000 // A number, not a date
},
MAX_VALUE: {
value: 8640000000000000
}
});

That defines properties on Date that cannot be changed that have the min/max numeric value for dates. (On a JavaScript engine that has ES5 support.)

Determining the minimum and maximum date in an array object

Sort the array by date and pick the first and the last ones :

const dates = [  { settledDate: "12-19-2018" },  { settledDate: "12-12-2018" },  { settledDate: "10-19-2018" },  { settledDate: "10-12-2018" }];
const sorted = dates.sort((a, b) => new Date(a.settledDate) - new Date(b.settledDate));
const minDate = sorted[0];const maxDate = sorted.reverse()[0];
console.log('maxDate : ', maxDate.settledDate);console.log('minDate : ', minDate.settledDate);

Find the minimum date between two maximum dates based off unique values in a column

We may use slice_max after grouping by 'subproduct', pull the date and get the min, assign it to a new object

library(dplyr)
dfone %>%
group_by(subproduct) %>%
slice_max(n = 1, order_by = date) %>%
ungroup %>%
pull(date) %>%
min -> Min_date

-output

 Min_date
[1] "2021-05-01"

Another option is to arrange the rows and filter using duplicated

dfone %>%
arrange(subproduct, desc(date)) %>%
filter(!duplicated(subproduct)) %>%
pull(date) %>%
min

Extract minimum and maximum date using dplyr

Could try:

library(dplyr)

df %>%
mutate_at(vars(-Id), list(~ as.Date(as.character(.)))) %>%
mutate(pmx = pmax(!!! syms(select(., contains("max")) %>% names), na.rm = T),
pmn = pmin(!!! syms(select(., contains("min")) %>% names), na.rm = T),
diffr = pmx - pmn,
pmx = case_when(diffr > 365 ~ a_max_date_df1, TRUE ~ pmx),
pmn = case_when(diffr > 365 ~ a_min_date_df1, TRUE ~ pmn)
)

Output:

  Id a_min_date_df1 a_max_date_df1 a_min_date_df2 a_max_date_df2 a_min_date_df3 a_max_date_df3 a_min_date_df4
1 1 2014-01-01 2014-01-10 <NA> <NA> <NA> <NA> 2014-02-20
2 2 2014-02-01 2014-02-10 <NA> <NA> 2015-02-20 2015-03-01 <NA>
a_max_date_df4 pmx pmn diffr
1 2014-05-01 2014-05-01 2014-01-01 120 days
2 <NA> 2014-02-10 2014-02-01 393 days

Minimum and maximum dates within continuous date range grouped by name

Here is an example using an ad-hoc tally table

Example or dbFiddle

;with cte as (
Select A.[Name]
,B.D
,Grp = datediff(day,'1900-01-01',D) - dense_rank() over (partition by [Name] Order by D)
From YourTable A
Cross Apply (
Select Top (DateDiff(DAY,StartDate,EndDate)+1) D=DateAdd(DAY,-1+Row_Number() Over (Order By (Select Null)),StartDate)
From master..spt_values n1,master..spt_values n2
) B

)
Select [Name]
,StartDate= min(D)
,EndDate = max(D)
From cte
Group By [Name],Grp

Returns

Name    StartDate   EndDate
MIKE 2019-05-15 2019-05-18
MIKE 2020-05-18 2020-05-19

Just to help with the Visualization, the CTE generates the following

Sample Image

How to select Maximum and minimum date values and pass those as a query

Use a scalar subquery to find the maximum date across the whole table:

SELECT *
FROM dbo.MyTable
WHERE TransDate >= '2012-12-01' AND
TransDate < (SELECT DATEADD(DAY, 1, MAX(TransDate)) FROM dbo.MyTable);

Note that I am using a strict inequality (less than) in the WHERE clause against one day later than the max date. This will include all days which fall on or earlier than the maximum date.

How to find min and max date across columns in SAS data set?

If the variables are actual date values.

data have;
input subjid $ (var1-var6) (:date.);
format var1-var6 date9.;
datalines;
121 23jan2022 24jan2022 20jan2022 24jan2022 26jan2022 25jan2022
122 20jan2022 22jan2022 26jan2022 28jan2022 23jan2022 27jan2022
;

Then just use the MIN() and MAX() functions to find the min and max dates. Once you have those simple subtraction will find the difference (range of dates).

data want;
set have;
mindate = min(of var1-var3);
maxdate = max(of var4-var6);
diff = maxdate-mindate;
format mindate maxdate date9.;
run;

Results

Sample Image

Find minimum and maximum date for what a person can be born for a certain age

If you are 33 as of today (2018-02-22), it means either

  • It's your birthday today, you just turn 33: Your birthday is 1985-02-22
  • Today is your last day as 33, tomorrow is your 34th birthday. Your birthday should be 1984-02-23

So we know that the date range should be from 1984-02-23 to 1985-02-22, which translates to:

// Today = 2018-02-22

// Maximum = Today - 33 years ago
// = 2018-02-22 - 33 years
// = 1985-02-22
$max = Carbon::today()->subYears($age)->toDateString();

// Minimum = Tomorrow - 34 years ago
// = 2018-02-23 - 34 years
// = 1984-02-23
$min = Carbon::tomorrow()->subYears($age + 1)->toDateString();


Related Topics



Leave a reply



Submit