I am using AntD Range Picker. I want to allow the user to select only 90 days, starting from 90 days from today. I have mange to block all the future days (after today) with the following code. How can I allow only 3 months (3 months back from today)?
<RangePicker
format="YYYY-MM-DD"
onChange={onChange}
disabled={loading}
onOpenChange={onOpenChange}
disabledDate={(currentDate) => currentDate.isAfter(moment())}
/>
Try this:
function disabledDate(current) {
return current > moment() || current < moment().subtract(3, 'day');
}
<DatePicker disabledDate={disabledDate} />
the first condition will disable the future days and the second one will enable 3 dates backward from today. In your case, you can change it to 90.
Related
Trying different things and combos, but can't get it right. Times and dates always get me.
I'm simply trying to compare now() and a date/time and show "in 2 days 6 hrs 30 mins" or "3 days 1 hr 20 mins ago" and I'm guessing someone has done this already?
So, let's say the date in column A is 2023-01-24 10:00 and now it is 2023-01-25 11:05, then Col B should say "1 day 1 hr 5 mins ago"
I've tried duration(), date/time formatting the cell, days(..), but I can't find something that works reliably.
Another thing you could try:
=INT(NOW()-A1)&" d "&TEXT(NOW()-A1,"h \hr m \min a\go")
=NOW()-A1 will actually calculate the difference between Now and that time, BUT consider that the time is updated only when you make a change, it's not a countdown second by second. You can add an updater per minute but not more than that. Go to File - Settings and set On change and every minute:
If you need a specific format like that you mentioned you should then do calculations to add them. If you only need days, hours, minutes and seconds, you can do the next checkings and concatenations:
=LAMBDA(dif,
IF(INT(dif),INT(dif)&" days ","")
&IF(HOUR(dif),HOUR(dif)&" hours ")
&IF(MINUTE(dif),MINUTE(dif)&" minutes ")
&IF(SECOND(dif),SECOND(dif)&" seconds")&" ago")
(Now()-A1)
please try:
=TRIM(LAMBDA(y,IF(NOW()-A1<1,"0 days",IF(y=1,y&" "&"day",y&" "&"days")))(DATEDIF(A1,NOW(),"D"))&" "&
LAMBDA(y,IF(y=1,y&" "&"hr",y&" "&"hrs"))(HOUR(NOW()-A1))&" "&
LAMBDA(y,IF(y=1,y&" "&"min",y&" "&"mins"))(MINUTE(NOW()-A1))&" ago")
try:
=INDEX(IF(ISDATE_STRICT(A2:A), TRIM(FLATTEN(QUERY(TRANSPOSE(
IFERROR(LAMBDA(a, LAMBDA(x, IF(x=0,,IF(x>1, x&a&"s", x&a)))
(DATEDIF(A2:A, NOW(), {"Y", "YM", "MD"})))({" year", " month", " day"}))),,9^9))), ))
In Pine Script, how do I find the price based on a certain number of days ago? I've tried something like this...
// Find the price 90 days ago
target = time - 90 * 60 * 60 * 24 * 1000
valuewhen(time < target, close, 1)
...however time < target never seems to return true – presumably because the current bar's time cannot also be in the past at the same time. Perhaps valuewhen() wasn't designed to be used with dynamic values that change on every bar?
Do I need to use a loop instead, and scan through every past bar until I find the date I'm looking for?
Perhaps there's a better way, but the workaround I'm using currently using is a function with a for loop, scanning backwards until the appropriate date is found. Here is my function:
priceXDaysAgo(numDays) =>
targetTimestamp = time - numDays*60*60*24*1000
// Declare a result variable with a "void" value
float result = if false
1
// We'll scan backwards through the preceding bars to find the first bar
// earlier than X days ago (it might be a little greater than X days if
// there was a break in trading: weekend, public holiday, etc.)
for i = 1 to 1000
if time[i] < targetTimestamp
result := close[i]
break
result
You can then call the function anywhere in your script:
priceXDaysAgo(90)
I need to show last Sunday's date in a cell for a weekly report that I'm creating on google sheets. I've been googling to find a solution and the closest I found is this:
=TODAY()+(7-WEEKDAY(TODAY(),3))
But this gives next Monday's date. Any idea how to modify this to show last Sunday's date? Alternately, do you have another way to solve this problem?
The formula you're looking for would be:
=DATE(YY, MM, DD) - (WEEKDAY(DATE(YY, MM, DD)) - 1) - 7
// OR
=TODAY() - (WEEKDAY(TODAY()) - 1) - 7
Depending on what you take to be "last Sunday," you can simplify/factor this:
If you take "last Sunday" to mean, "the Sunday which has most recently happened:"
=DATE(A2,B2,C2) - WEEKDAY(DATE(A2,B2,C2)) + 1
If you take "last Sunday" to mean, "the Sunday which took place during the previous week:"
=DATE(A4,B4,C4) - WEEKDAY(DATE(A4,B4,C4)) - 6
Working through it:
=TODAY() - (WEEKDAY(TODAY()) - 1) - 7
TODAY()
// get today's date, ie. 22/11/2019
WEEKDAY(TODAY())
// get the current day of the week, ie. 6 (friday)
-
// take the first date and subtract that to rewind through the week,
// ie. 16/11/2019 (last saturday, since saturday is 0)
- 1
// rewind back one less than the entire week
// ie. 17/11/2019 (this sunday)
- 7
// rewind back to the sunday before this
// sunday, ie. 10/11/2019
Hopefully this explanation explains what the numbers at the end are doing. + 1 takes you from the Saturday of last week to the Sunday of the current week (what I would call "this Sunday"), and - 6 takes you back through last week to what I would call "last Sunday."
See here:
try:
=ARRAYFORMULA(TO_DATE(FILTER(ROW(INDIRECT(VALUE(TODAY()-6)&":"&VALUE(TODAY()))),
TEXT(ROW(INDIRECT(VALUE(TODAY()-6)&":"&VALUE(TODAY()))), "ddd")="Sun")))
if today is Sunday and you want still the last Sunday then:
=ARRAYFORMULA(IF(TEXT(TODAY(), "ddd")="Sun", TODAY()-6,
TO_DATE(FILTER(ROW(INDIRECT(VALUE(TODAY()-6)&":"&VALUE(TODAY()))),
TEXT(ROW(INDIRECT(VALUE(TODAY()-6)&":"&VALUE(TODAY()))), "ddd")="Sun"))))
Using monday as 1 index, this will return the previous sunday or today if it is sunday
=if((7-WEEKDAY(today(),2))>0,today()-(7-(7-WEEKDAY(today(),2))),today()+(7-WEEKDAY(today(),2)))
One can select for other days of the week by changing the number "7" directly before "-WEEKDAY(today(),2)" in the three places that pattern exists.
I am trying to enable/disable a command button based on the value of a textbox. Ex. "08-09-2015 15:06:24", taken from a table column field
It seems that it will either enable OR disable it, depending on < or >.
I want it to find out if txt.Value is MORE than 15 hours ago, then it should activate the button. If not, leave it "false"
The textbox and command button are on the same form.
This is what I have so far, and apparently not working.
Public Sub Kommandoknap184_Click()
If Me.txtOpdTid.Value < DateAdd("h", -15, Date) Then
Kommandoknap35.Enabled = False
Else
Kommandoknap35.Enabled = True
End If
End Sub
Date() gives you the current date with midnight as the time. Now() gives you the current date and time. So I think you want Now() instead of Date().
Public Sub Kommandoknap184_Click()
If Me.txtOpdTid.Value < DateAdd("h", -15, Now) Then
' Value is MORE than 15 hours ago, then it should activate the button
Me.Kommandoknap35.Enabled = True
Else
Me.Kommandoknap35.Enabled = False
End If
End Sub
It looks like there are two problems with your code.
In the DateAdd function you want to use Now() instead of Date(). Now() will include the current time along with the date. This is important since you are comparing the number of hours and not the number of days.
The enable/disable logic is backwards (enabling the button when it should be disabled and vice-versa).
Below is a simplified version with the corrections:
Public Sub Kommandoknap184_Click()
Dim isMoreThan15HoursAgo As Boolean
isMoreThan15HoursAgo = Me.txtOpdTid < DateAdd("h", -15, Now)
Me.Kommandoknap35.Enabled = isMoreThan15HoursAgo
End Sub
You might also consider having this code run in the After Update event of the text box instead of when clicking a separate button. This would make the enable/disable of the button more seamless.
If I've got a time_t value from gettimeofday() or compatible in a Unix environment (e.g., Linux, BSD), is there a compact algorithm available that would be able to tell me the corresponding week number within the month?
Ideally the return value would work in similar to the way %W behaves in strftime() , except giving the week within the month rather than the week within the year.
I think Java has a W formatting token that does something more or less like what I'm asking.
[Everything below written after answers were posted by David Nehme, Branan, and Sparr.]
I realized that to return this result in a similar way to %W, we want to count the number of Mondays that have occurred in the month so far. If that number is zero, then 0 should be returned.
Thanks to David Nehme and Branan in particular for their solutions which started things on the right track. The bit of code returning [using Branan's variable names] ((ts->mday - 1) / 7) tells the number of complete weeks that have occurred before the current day.
However, if we're counting the number of Mondays that have occurred so far, then we want to count the number of integral weeks, including today, then consider if the fractional week left over also contains any Mondays.
To figure out whether the fractional week left after taking out the whole weeks contains a Monday, we need to consider ts->mday % 7 and compare it to the day of the week, ts->wday. This is easy to see if you write out the combinations, but if we insure the day is not Sunday (wday > 0), then anytime ts->wday <= (ts->mday % 7) we need to increment the count of Mondays by 1. This comes from considering the number of days since the start of the month, and whether, based on the current day of the week within the the first fractional week, the fractional week contains a Monday.
So I would rewrite Branan's return statement as follows:
return (ts->tm_mday / 7) + ((ts->tm_wday > 0) && (ts->tm_wday <= (ts->tm_mday % 7)));
If you define the first week to be days 1-7 of the month, the second week days 8-14, ... then the following code will work.
int week_of_month( const time_t *my_time)
{
struct tm *timeinfo;
timeinfo =localtime(my_time);
return 1 + (timeinfo->tm_mday-1) / 7;
}
Assuming your first week is week 1:
int getWeekOfMonth()
{
time_t my_time;
struct tm *ts;
my_time = time(NULL);
ts = localtime(&my_time);
return ((ts->tm_mday -1) / 7) + 1;
}
For 0-index, drop the +1 in the return statement.
Consider this pseudo-code, since I am writing it in mostly C syntax but pretending I can borrow functionality from other languages (string->int assignment, string->time conversion). Adapt or expand for your language of choice.
int week_num_in_month(time_t timestamp) {
int first_weekday_of_month, day_of_month;
day_of_month = strftime(timestamp,"%d");
first_weekday_of_month = strftime(timefstr(strftime(timestamp,"%d/%m/01")),"%w");
return (day_of_month + first_weekday_of_month - 1 ) / 7 + 1;
}
Obviously I am assuming that you want to handle weeks of the month the way the standard time functions handle weeks of the year, as opposed to just days 1-7, 8-13, etc.