I'm working with V-calendar to select date ranges
The default v-calendar view shows me the current month and the next month
(today is febury), like:
However I would like to set default view from current and past month, cause the default range is the last 30 days
, like:
How can I set this in my v-calendar?
I just solved it by setting the from-page and to-page props. like
<DatePicker :from-page="fromPage" :to-page="toPage" mode="range"/>
And use momentjs to get these parameters
get currentMonth() {
const month = moment().month()+1; // 0 to 11
const year = moment().year();
return { month, year };
}
get previousMonth() {
const month = moment().subtract(1, "month").month()+1; // 0 to 11
const year = moment().subtract(1, "month").year();
return { month, year };
}
Related
Working in Google Scripts, I'm trying to create a function that will look at the names of all tabs in a Google Sheet, and delete all sheets that meet these conditions: 1) tab name include a date (sheet names that do have a date are prefaced with some other text - the mm/dd/yyyy formatted date is in the string) and 2) the date in that sheet name is older than today's date minus 2 days).
There are two sheet names that include dates: "Leadership Review mm/dd/yyyy" and "Leadership Review w/notes mm/dd/yyyy". I have a script that auto-runs DAILY to create these sheets, so the goal is to automate a one-time clean-up for old sheets and set up a daily trigger to auto-run that function.
So far, I've created an array to capture the names of each sheet name ("tabNameArray") and have a regexp for use in matching for mm/dd/yyyy text that shows up in a sheet name.
My thought on how this would work - not sure how to accomplish 2 and 3:
create that array
parse the array - match each sheetname in the array against the regexp mm/dd/yyyy to identify sheetnames with a date.
A loop through that array... IF that sheetname has a date (create a new array or subarray with just those? doesn't seem necessary, but a thought), AND that date is > 2 days from today(), delete those sheets.
function deleteOldReportSheets() {
var sheetNameArray = new Array();
var sheetWithDatesArray = new Array(); //not sure this is necessary
var dateRegex = new RegExp("[0-3]?[0-9]\/[0-3]?[0-9]\/(?:[0-9]{2})?[0-9]{2}"); //for mm/dd/yyyy match, tested successfully on regex101.com
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i=0 ; i<sheets.length ; i++) sheetNameArray.push( [ sheets[i].getName() ] ) // populates the sheetNameArray with names of each sheet
(do this: for sheets that have a date that is older than 2 days from today, delete...)
I'd appreciate support to code this - even better if there's a more efficient way than what I started thinking through.
Thanks for your assistance.
Description
The following example will compare the named sheets containing the date to today and indicate which sheets should be deleted. I leave the deletion operation to the OP.
Script
function test() {
try {
let spread = SpreadsheetApp.getActiveSpreadsheet();
let sheets = spread.getSheets();
let dateRegex = new RegExp("[0-3]?[0-9]\/[0-3]?[0-9]\/(?:[0-9]{2})?[0-9]{2}"); //for mm/dd/yyyy match, tested successfully on regex101.com
let today = new Date();
console.log("today = "+Utilities.formatDate(today,"GMT","MM/dd/yyyy"));
today = new Date(today.getFullYear(),today.getMonth(),today.getDate()-2);
for( let i=0; i<sheets.length; i++ ) {
let sheet = sheets[i];
let name = sheet.getName();
console.log("Sheet name = "+name);
let match = name.match(dateRegex);
if( match ) {
match = new Date(match[0]);
if( match.valueOf() <= today.valueOf() ) {
console.log("delete");
}
else {
console.log("keep");
}
}
}
}
catch(err) {
console.log(err);
}
}
Console.log
11:45:16 AM Notice Execution started
11:45:17 AM Info today = 05/01/2022
11:45:17 AM Info Sheet name = Sheet1
11:45:17 AM Info Sheet name = Sheet 04/29/2022
11:45:17 AM Info delete
11:45:17 AM Info Sheet name = Sheet 04/30/2022
11:45:17 AM Info keep
11:45:17 AM Info Sheet name = Sheet 05/01/2022
11:45:17 AM Info keep
11:45:16 AM Notice Execution completed
Reference
Date()
String.match()
I think it can be something like this:
function myFunction() {
const reg = new RegExp(/\d{2}\/\d{2}\/\d{4}/);
const ss = SpreadsheetApp.getActiveSpreadsheet();
const if_two_days_ago = date =>
new Date(date).valueOf() < new Date().valueOf() - 3600000 * 48;
ss.getSheets()
.filter(s => reg.test(s.getName()))
.filter(s => if_two_days_ago(s.getName().match(reg)))
.forEach(s => { console.log('bye-bye -->', s.getName()); ss.deleteSheet(s) });
}
Delete Sheets with dates older than two days
function delSheets() {
const ss = SpreadsheetApp.getActive();
const dt = new Date();
const dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() - 2).valueOf();//date threshold value
ss.getSheets().forEach(sh => {
let m = sh.getName().match(/\d{1,2}\/\d{1,2}\/\d{4}/g);//includes a date
if(m && new Date(m[0]).valueOf() < dtv) {
ss.deleteSheet(sh);
}
});
}
I am having trouble getting "hours" int value from hive box and get the sum of all hours total and display it in a card as a String hours. what I have is:
var data = fastBox.values.where((element) => element.hours !=
0).toList();
if (fastBox.isNotEmpty){
int hoursInt = data.reduce((value, element) => {
value.hours + element.hours
});
hours = "${hoursInt.toString()}";
} else{
hours = "0 " + Languages.of(context)!.hours;
}`
i am getting error:
Class 'List<dynamic>' has no instance getter 'hours'.
Receiver: Instance(length:1) of '_GrowableList'
Tried calling: hours
my hive is:
#HiveType(typeId: 4)
class FastHive{
#HiveField(0)
final int days;
#HiveField(1)
final List<DateTime> dates;
#HiveField(2)
final int hours;
#HiveField(3)
DateTime startTime;
#HiveField(4)
DateTime endTime;
FastHive(this.days, this.dates, this.hours, this.startTime,
this.endTime);
}
just learning here. pls, help! Thank you!
I have an API integration for our web store running on AWS Lambda to return live delivery quotes based on customer address, and then create the delivery order to a third party delivery as a service provider when the invoice is completed (paid).
I was able to add a time restriction for Monday-Saturday but Sunday has different hours and is not working. Here is the relevant code:
'use strict'
/**
* This function is use to generate qoutes to client 1st warehouse
*/
exports.handler = function (event, context, callback) {
console.log('-------------------EVENT OBJECT--------------------------')
// console.log(event.body.shipping_address)
console.log(event)
try {
const app = require('./app')
const EventEmitter = require('events').EventEmitter
const _bus = new EventEmitter()
let date = new Date()
if (date.getDay() == 0) {
if (!(date.getHours() >= 17 && date.getHours() <= 22) || !(date.getHours() < 3)) {
callback(null, {
message: 'The store is closed'
})
}
} else {
if (date.getHours() >= 3 && date.getHours() <= 15) {
callback(null, {
message: 'The store is closed'
})
}
}
let _shipmentReturn = []
let _shipmentReturnError = []
}
catch(e) {
}
}
Be very careful when using NOT logic.
Your 'normal' days have the store closed from 3am to 4pm. (Yes, 4pm. That's because you only check hours, so 3:59pm is still an 'hour' of 3, so it would be closed.)
On Sunday, the store is closed from midnight to 4:59pm, and also 10pm to midnight.
Take a look at this line:
if (!(date.getHours() >= 17 && date.getHours() <= 22) || !(date.getHours() < 3)) {
Let's pick a time of 2am. It equates to:
if (!(FALSE) || !(TRUE))
This equals TRUE, so the store is closed.
Same for 4am: if (!(FALSE) || !(FALSE)) also equals TRUE
You possibly want an AND rather than an OR in those logic statements.
I would also recommend that you convert the UTC times into your "local" times, which would make it easier for you to write the logic. This will avoid errors where UTC Sunday does not actually align to your 'local' Sunday. For example, if you are UTC-6, then 2am UTC Sunday is not Sunday in your timezone.
I have two dates and I want to compare it.
How can I compare dates?
I have to date objects. Say modificateionDate older updatedDate.
So which is the best practice to compare dates?
Date now conforms to Comparable protocol. So you can simply use <, > and == to compare two Date type objects.
if modificateionDate < updatedDate {
//modificateionDate is less than updatedDate
}
Per #NiravD's answer, Date is Comparable. However, if you want to compare to a given granularity, you can use Calendar's compare(_:to:toGranularity:)
Example…
let dateRangeStart = Date()
let dateRangeEnd = Date().addingTimeInterval(1234)
// Using granularity of .minute
let order = Calendar.current.compare(dateRangeStart, to: dateRangeEnd, toGranularity: .minute)
switch order {
case .orderedAscending:
print("\(dateRangeEnd) is after \(dateRangeStart)")
case .orderedDescending:
print("\(dateRangeEnd) is before \(dateRangeStart)")
default:
print("\(dateRangeEnd) is the same as \(dateRangeStart)")
}
> 2017-02-17 10:35:48 +0000 is after 2017-02-17 10:15:14 +0000
// Using granularity .hour
let order = Calendar.current.compare(dateRangeStart, to: dateRangeEnd, toGranularity: .hour)
> 2017-02-17 10:37:23 +0000 is the same as 2017-02-17 10:16:49 +0000
Swift iOS 8 and up When you need more than simply bigger or smaller date comparisons. For example is it the same day or the previous day,...
Note: Never forget the timezone. Calendar timezone has a default, but if you do not like the default, you have to set the timezone yourself. To know which day it is, you need to know in which timezone you are asking.
extension Date {
func compareTo(date: Date, toGranularity: Calendar.Component ) -> ComparisonResult {
var cal = Calendar.current
cal.timeZone = TimeZone(identifier: "Europe/Paris")!
return cal.compare(self, to: date, toGranularity: toGranularity)
}
}
Use it like this:
if thisDate.compareTo(date: Date(), toGranularity: .day) == .orderedDescending {
// thisDate is a previous day
}
For a more complex example how to use this in a filter see this:
https://stackoverflow.com/a/45746206/4946476
Swift has ComparisonResult with orderedAscending, orderedDescending and same as below.
if modificateionDate.compare(updatedDate) == ComparisonResult.orderedAscending {
//Do what you want
}
Hope this may help you.
How can I get the first day of the week of any given year (day being 1 to 7, or weekday name)?
I tried to figure it out in JavaScript, but I accept any other language.
I need to select a year to later build the full calendar (I thought using HTML tables and JavaScript), and for that I need to know at least the first day of the selected year.
I haven't found a solution or a question specifically dealing with finding the first day of any given year such that you only need to pass 1995, 2007, 1891. So if it's a repeated question please point the solution.
Do you have at least an online chart or DHTML site where I can see any full calendar for any year visually in that way?
In Javascript you can use this:
getWeekDay = function (year) {
var d = new Date();
d.setFullYear(year,0,1);
return d.getDay()+1;
};
document.write(getWeekDay(2011));
Result is 1..7, as requested.
At this Wikipedia article, look for Sakamoto's algorithm.
Pretty easy with C# and .NET:
using System;
public class SomeClass
{
public static void Main()
{
DateTime dt = new DateTime(2000,1,1);
Console.WriteLine(dt.DayOfWeek);
dt = new DateTime(2010,1,1);
Console.WriteLine(dt.DayOfWeek);
}
}
Output:
Saturday
Friday
The Java version:
import java.util.Date;
import java.util.GregorianCalendar;
public class test3
{
public static void main (String[] args)
{
GregorianCalendar c = new GregorianCalendar(2000,1,1);
Date d = c.getTime();
System.out.println(d.getDay());
c = new GregorianCalendar(2010,1,1);
d = c.getTime();
System.out.println(d.getDay());
}
}
Output:
2
1
With .NET's BCL:
return new DateTime(year, 1, 1).DayOfWeek; // DayOfWeek enum value
In Noda Time:
return new LocalDate(year, 1, 1).IsoDayOfWeek; // IsoDayOfWeek enum value
In Java using the built-in classes:
Calendar calendar = Calendar.getInstance();
calendar.set(year, 1, 1);
return calendar.get(Calendar.DAY_OF_WEEK); // 1 (Sunday) - 7 (Saturday)
In Java using Joda Time:
return new LocalDate(year, 1, 1).getDayOfWeek(); // 1 (Monday) - 7 (Sunday)
This site http://5dspace-time.org/Calendar/Algorithm.html
has a good explanation on how to calculate it even with pencil-and-paper
Wikipedia explains it too