I am trying to validate or check the date range by passing month and year, If date range come under the given month range between, then it would be valid else error.
month = "04, 06, 2022"; // from-month, to-month, year
date = "12-05-2022" // DD-MM-YYYY
from month = 04
to month = 06
year = 2022
I read the documentation but didn't achive the goal.
I want if I am passing this data below -
month = "04, 06, 2022"; // from-month, to-month, year
date = "12-07-2022" // DD-MM-YYYY => error
date = "12-05-2022" // DD-MM-YYYY => success
let value = '12-05-2022'; //DD-MM-YYYY
let check = moment(value,'DD-MM-YYYY', true).isValid();
console.log(check) //returns true
return check
Try to use a range to check if the date corresponding to value is contained:
const year = 2022;
const fromMonth = 4;
const toMonth = 6;
const startDate = new Date(year, fromMonth, 12);
const endDate = new Date(year, toMonth, 15);
const value = '12-05-2022';
const check = moment().range(startDate, endDate).contains(new Date(value));
console.log(check);
Related
Basically we are using a powerapp to do the register. We do this once a week and it populates a column called "Attendance" with the value "Present" For each child that attends.
However before the next register happens I require the attendance column to be clear. In order to do this I would require the attendance column to be copied to a new column with "today" date".
I have managed to create a column with "today" date but need help in copying the values over. You can see the code i have used to create a column. The attendance column is column M.
function main(workbook: ExcelScript.Workbook) {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
let x = dd + '/' + mm + '/' + yyyy;
let sh = workbook.getActiveWorksheet()
let tbl = sh.getTable("FBRegister")
tbl.addColumn().setName(x)
}
You can try the following script:
function main(workbook: ExcelScript.Workbook) {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
let x = dd + '/' + mm + '/' + yyyy;
let sh = workbook.getActiveWorksheet()
let tbl = sh.getTable("FBRegister")
let todayColumn = tbl.addColumn(); // Save the new column
todayColumn.setName(x); // Set the name to today's date
// Get the attendance column, excluding the header
let attendance = tbl.getColumn('Attendance').getRangeBetweenHeaderAndTotal();
// Copy values from attendance column to the new column
todayColumn.getRangeBetweenHeaderAndTotal().setValues(attendance.getValues());
// Clear attendance column
attendance.clear();
}
I've assumed the attendance column is named "Attendance", but if not, you can modify that line (let attendance = ... ) accordingly. Additionally, the last line of code clears the attendance column once the values have been copied over, but you can comment out/delete that line if you don't want that.
Hopefully that helps!
I am parsing an excel document in excel using the excel: ^1.1.5 package. In my sheet, i have a Date column and this Date is being received in my Flutter code in the following format
"44663"
rather than:
"2022/04/12"
How do I parse this to a format such as YY-MM-DD.
I have tried DateTime.parse(), but it throws an error that my date format is invalid.
I found the answer :
const gsDateBase = 2209161600 / 86400;
const gsDateFactor = 86400000;
final date = double.tryParse("44663");
if (date == null) return null;
final millis = (date - gsDateBase) * gsDateFactor;
print(DateTime.fromMillisecondsSinceEpoch(millis.toInt(), isUtc: true));
I have a simple spreadsheet listing certificates and expiry dates. Before we moved this to sharepoint online we had a macro than on opening the spreadsheet would check dates in a range of cells and highlight those within three months. It was intended to highlight anything up for renewal before it expired.
I appreciate macros are not an option in Excel online. Can this (or something very similar) be achieved in Office Scripting?
It should be possible to create an Office Script that highlights cells with a date that is within three months of the present.
Something Like:
function main(workbook: ExcelScript.Workbook)
{
// highlight all days between today and the next number of days out
// Number of days to look ahead
const daysOut = 90;
// Excel by default stores dates as the number of days after January 1, 1900
const dayMin = currentDaysSince1900();
const dayMax = dayMin + daysOut;
// Need to target the column to look at and how far down the column
const columnToLookAt = "A";
const rowStart = 1;
const rowEnd = 4;
const rangeAddress = `${columnToLookAt}${rowStart}:${columnToLookAt}${rowEnd}`;
const sheet = workbook.getActiveWorksheet();
// get range column
const range = sheet.getRange("A1:A3");
const values = range.getValues();
// iterate through the rows of values
for (let i =0 ; i < values.length; i++) {
const value = values[i][0];
console.log(value);
if (typeof value === "number") {
// only look at numbers
if (value >= dayMin && value <=dayMax ) {
// highlight
const rangeAddress = `${columnToLookAt}${rowStart +i}`;
const range = sheet.getRange(rangeAddress);
range.getFormat().getFill().setColor("yellow");
}
}
}
}
/**
* Current Days since Jan 1 1900
* Equivalent to number of current excel day
*/
function currentDaysSince1900() {
// method returns the number of milliseconds elapsed since January 1, 1970
const nowMilliseconds = Date.now();
const millisecondsPerDay = 24 * 60 * 60 * 1000 ;
const nowDays = Math.floor(nowMilliseconds / millisecondsPerDay);
const daysBetween1900And1970 = 25567;
const elapsed = nowDays + daysBetween1900And1970 +2; // add two to include both jan 1s
return elapsed;
}
In terms of triggering the script:
Office Script does not currently support running a script on opening a workbook.
You can manually trigger the script whenever you like.
You can also create a Microsoft Power Automate flow to run the script every day to keep the workbook updated.
More Office Script resources:
Official Microsoft Office Script Date Example
Official Microsoft Office Script Examples
wandyezj office script examples
I have start time and end time as follows
var starttime=1631108701000
var endtime=1631116762000
var sessionstart=moment.unix(startTime);
var sessionend=moment.unix(endTime);
var ms = moment(sessionend,"DD/MM/YYYY HH:mm:ss").diff(moment(sessionstart,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var timeelapsed = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
goal is to display the starttime and endtime with proper date time stamp and disply the difference between then like 2hours16minutes53seconds or 02:16:53.The above code returns faulty data. How do I fix it?
The below code worked for me
var sessionstart= moment.unix(startTime/1000).format("DD/MM/YYYY HH:mm:ss");
var sessionend= moment.unix(endTime/1000).format("DD/MM/YYYY HH:mm:ss");
var ms = moment(sessionend,"DD/MM/YYYY HH:mm:ss").diff(moment(sessionstart,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var timeelapsed = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
By default moment.unix() expects in seconds so we need to divide by 1000 and format as per requirement.
You could also have a look at this approach. First convert unix epoch milliseconds to seconds. Then use moment's diff function to calculate the respective units. Notice that I am adding corresponding units after each diff to get the accurate difference.
const startTime = "1631108701000";
const endTime = "1631116762000";
// Converting epoch milliseconds to seconds
const startUnixTime = moment.unix(startTime / 1000);
const endUnixTime = moment.unix(endTime / 1000);
// hour difference
const hourDiff = endUnixTime.diff(startUnixTime, "hours");
startUnixTime.add(hourDiff, "hours");
// minute difference
const minDiff = endUnixTime.diff(startUnixTime, "minutes");
startUnixTime.add(minDiff, "minutes");
// second difference
const secDiff = endUnixTime.diff(startUnixTime, "seconds");
// Actual Input dates
const startDate = moment(startUnixTime).format("DD-MM-YYYY hh:mm:ss");
const endDate = moment(endUnixTime).format("DD-MM-YYYY hh:mm:ss");
console.log("Input start date", startDate);
console.log("Input end date", endDate);
// Difference in hours, minutes, seconds
console.log(hourDiff, minDiff, secDiff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
I am coding in NodeJs and have to create a filename which have date and time appended. The format should be "metadata_yyymmdd_his.json". I am struggling to get the date in dd and month in MM format.
Below is the code that I have written as of now.
const dateObj = new Date();
const month = (`0${dateObj.getUTCMonth() + 1}`).slice(-2);
const date = (`0${dateObj.getUTCDate()}`).slice(-2);
const metadataFileName = 'metadata_' + dateObj.getUTCFullYear() + month + date + "_" + dateObj.getUTCHours() + dateObj.getUTCMinutes() + dateObj.getSeconds() + '.json'
How can I get the date and month prepended with 0 if it is single digit?
EX: if month is April then the result should be 04
If you're in node, don't try to roll your own datetime parsing/formatting: install and then require something that's been written specifically for that purpose, and has been tested extensively so you can rely on it working properly. A common package for that is moment.js, using its .format() function
const now = moment.now();
const formatted = moment().format("MM-DD") // or whatever other formatting you need