Trying to calculate the end of the next day in node.js - node.js

I'm trying to calculate the end of the next day for any given date as an expiry date.
So if the input is eg 10/26/2013 16:36:46 then I'd like to get 10/27/2013 23:59:59 and 7/31/2013 16:36:46 would give me 8/1/2013 23:59:59
It gets obviously difficult with leap years and when the next day is a new month. So I tried to use the dateParse function which gives me a number that I can easily add 86400000 (full day in milliseconds) and then I want to use basically the reverse of dateParse (turn a number into a date object) so that I can then take out the date component and replace the time component with 23:59:59
Here's my code which doesn't work - setTime doesn't seem to be the right function and makes my next_day variable "undefined"
var next_day = call_start.dateParse + 86400000; // adding a full day to it
next_day = next_day.setTime;
var day = next_day.getDate;
day = (day < 10 ? "0" : "") + day;
var month = next_day.getMonth + 1;
month = (month < 10 ? "0" : "") + month;
var year = next_day.getFullYear;
var hour = 23;
var min = 59;
var sec = 59;
var expiry = year + ":" + month + ":" + day + ":" + hour + ":" + min + ":" + sec;
console.log ('Streak expires at: ' + expiry);

You can use the set* functions to modify the date:
var date = new Date('7/31/2013 16:36:46');
date.setDate(date.getDate() + 1);
date.setHours(23);
date.setMinutes(59);
date.setSeconds(59);
console.log(date);
// Thu Aug 01 2013 23:59:59 GMT+0200 (CEST)

Related

Is there any way to display time format with am/pm in mongodb

time :
{ $dateToString: { format: "%H:%M:%S:%L%z", date: "$generated_on", timezone: "+05:30" }}
with the help of this time is generated but I want time in AM/PM format}\
So basically, for time being (version 5), Mongo DB does not support the AM/PM format on the date object, you have two options:
option one: Store the correct format in the first place
option two(heavy operation): store a JS function inside the MongoDB for this custom format and call that in your aggregation pipeline, js function would be like this:
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
Add MongoDB custom JS function myCustomDate:
db.system.js.insertOne(
{
_id: "myCustomDate",
value: function (date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
}
);
and use this based on your requirements on mapReduce command

Generating bulk dates

I wanted to ask if anyone knows a way of generating bulk dates. I want to know about how can I generate all of the dates and times between 20130631_0000 till 20151231_2359
The month limit is 0-12
The day limit is 0-31
and the part you see after underscore is hour and minute, the hour limit is 00 till 23 and the minute is from 00 till 59. It's basically YYYYMMDD_HHMM
Few examples:
20130810_1154, 20140103_2357, 20150722_1049, 20140103_2358, 20140103_2359, 20140104_0000, 20140104_0001, 20140105_1159, 20140105_1200
If you simply want to print each date to the console:
let date = new Date(2013,6,31,0,0);
const endDate = new Date(2015,12,31,23,59);
const padWithZero = num => num.toString().padStart(2, '0');
while (date <= endDate) {
const year = date.getFullYear();
const month = padWithZero(date.getMonth()+1);
const day = padWithZero(date.getDate());
const hour = padWithZero(date.getHours());
const minute = padWithZero(date.getMinutes());
console.log(`${year}${month}${day}_${hour}${minute}`)
date = new Date(date.getTime() + 60000)
}
Try this
function convertStringToDate(date){
var year = date.substring(0,4)
var month = date.substring(4,6)
var day = date.substring(6,8)
var hour = date.substring(9,11)
var minute = date.substring(11,13)
return new Date(parseInt(year), parseInt(month), parseInt(day), parseInt(hour), parseInt(minute) )
}
function addMinute(date){
var new_date = date;
new_date.setMinutes(date.getMinutes()+1)
return new_date
}
function getDatesBetween(start_date_str, end_date_str){
var start_date = convertStringToDate(start_date_str)
var end_date = convertStringToDate(end_date_str)
dates = []
date = start_date
while(date.getTime() !== end_date.getTime()){
dates.push(new Date(date))
date = addMinute(date)
}
dates.push(end_date)
return dates
}
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
function convertDateToString(date){
var year = date.getFullYear().toString()
var month = pad(date.getMonth().toString(),2)
var day = pad(date.getDay().toString(),2)
var hour = pad(date.getHours().toString(),2)
var minutes = pad(date.getMinutes().toString(),2)
return year + month + day + "_" + hour + minutes
}
var start_date_str = "20130620_1224"
var end_date_str = "20130621 1224"
dates = getDatesBetween(start_date_str, end_date_str)
formatted_dates = dates.map(convertDateToString)
console.log(dates.length)
console.log(dates[dates.length-1])
console.log(formatted_dates.length)
console.log(formatted_dates[dates.length-1])

Individual start date per month

together
how do I get it in PowerPivot or as Dax formula out that for my internal / individual months the beginning of the month from 31.120.2018 for January 2019 receive?
So, that I get the January in the PowerPivot calendar table already from 31.12.2018 and then in the 4-4-5 weeks cycle?
So
31.12.2018 + 28 days = January
31.12.2018 + (28 days * 2) = February
31.12.2018 + (28 days * 2) + 7 days = March
Does anyone have an idea?
Best Regards
First I create a calendar table, this is the simple part, I took a random start and end for testing:
CalendarDates = CALENDAR(DATE(2012;12;20);DATE(2021;1;5))
Then I added the column WeekNumber, this is to the greorgian calendar. A bit complicated because microsoft simply starts counting with 1 on the first of january..
WeekNumber =
var wNrfirstDayJanNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;1);2)
var fullWeekNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;7);2) = 1 //If day 7 of week is still first week, its a full week
var wNr31Dex = WEEKNUM(DATE(CalendarDates[Date].[Year];12;31);2)
var wNr31DexPrevYear = WEEKNUM(DATE(CalendarDates[Date].[Year]-1;12;31);2)
var wNr = WEEKNUM(CalendarDates[Date];2)
var pbi4JanWeekNrNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;4);2)
var pbi4JanWeekNr = WEEKNUM(DATE(CalendarDates[Date].[Year];1;4);2)
var wNrComp = if (pbi4JanWeekNr =2; wNr - 1; wNr)
return if(NOT(fullWeekNextYear) && wNr = wNr31Dex;IF(pbi4JanWeekNrNextYear = 2; wNr31Dex;1);if(wNrComp = 0; wNr31DexPrevYear; wNrComp))
last step is to create the Month based on the week number:
Month =
var quater = FLOOR((CalendarDates[WeekNumber] - 1)/13; 1)
var quarterWeek = MOD(CalendarDates[WeekNumber] - 1 - (quater*13);13)
var monthNr = FLOOR(quarterWeek/4;1)
var month445 = if (monthNr = 3; 2; monthNr) + quater * 3 + 1
return if (month445 = 13; 12;month445)
Note: I corrected week 53 in the last quarter so it becomes 4-4-6. You need to do your own math if you want it different.
End result:

x pages Question: calculate working day Error

I try use computed field to calculate leave and working days. The error form show as below
script to count the working days
var nDateStart = session.createDateTime(document1.getValue("StartDate")).toJavaDate();
var nDateEnd = session.createDateTime(document1.getValue("EndDate")).toJavaDate();
var syear = #Year(nDateStart)
var smth = #Month(nDateStart)
var sday = #Day(nDateStart)
var startdate = #Date(syear,smth,sday)
var eyear = #Year(nDateEnd)
var emth = #Month(nDateEnd)
var eday = #Day(nDateEnd)
var enddate = #Date(eyear,emth,eday)
var weekends = "1:7"; //assuming your weekend is Saturday and Sunday
var holidays = "#Date(2019;8;12):#Date(2019;12;25)"; // assuming your holiday is 12 August and 25th December
var formula = "#BusinessDays(" + startdate + ";" + enddate + ";" + weekends + ";" + holidays + ")";
return session.evaluate(formula);
Date/time constants in Lotus formula language are enclosed in square brackets, which you can clearly see in the documentation for #BusinessDays. Try this:
var formula = "#BusinessDays([" + startdate + "];[" + enddate + "];" + weekends + ";[" + holidays + "])";
return session.evaluate(formula);
You need to calculate a valid formula before you can submit it to session.evaluate. The string you submit isn’t a valid formula. E.g the your parameter is just an unquoted string.
Remove the startdate and enddate and replace it with something like ”#Date(“+syear+”;”+smonth+”;”+”;”+sday+”)” that should give you the formula you seek.

convert my string to AM or PM hour in ActionScript 3

I've got a String variable for my time :
var $myTime:String;
//Some calculation
trace(myTime);
// Result is : 14:25 (for example)
I'm looking for a way to convert this string ("14:25" in this example) to a simple AM / PM format.
So in this example it would be, for example, 2 PM
Any idea how I can simply do that ?
You can use DateFormatter class to set your wished pattern.
You can write your code as follow:
var df:DateFormatter = new DateFormatter();
df.formatString = "YYYY-MM-DD L:NN:SS";
df.format(myTime);
Where L is HOUR with PM/AM; NN are the minutes SS seconds.
You can see the complete guide, about DateFormatter pattern, here
var myTime:Date = new Date();
var timeString:String;
if (myTime.getHours() > 12)
{
timeString = String((myTime.getHours() - 12) + ":" + myTime.getMinutes() + " PM");
}
if (myTime.getHours() == 12)
{
timeString = String(myTime.getHours() + ":" + myTime.getMinutes() + " PM");
}
if (myTime.getHours() < 12)
{
timeString = String(myTime.getHours() + ":" + myTime.getMinutes() + " AM");
}
trace(timeString);
That should do the trick.

Resources