Flex add and subtract six months from now - apache-flex

Here is the code I am trying
public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24
var sixMonthsInMilis:int = 182 * millisecondsPerDay;
var sixMonthsInTheFuture:Date = new Date();
sixMonthsInTheFuture.setTime(sixMonthsInTheFuture.getTime() + sixMonthsInMilis );
var sixMonthsInThePast:Date = new Date();
sixMonthsInThePast.setTime(sixMonthsInThePast.getTime() - sixMonthsInMilis);
The one thats meant to be in the past is a few days in the future, the other a few days in the past. A far cry from the 6 months I am after
Any Ideas?

var today:Date = new Date();
today.setMonth(today.getMonth() + 6);
var sixMonthsInTheFuture:Date = today;
var anotherToday:Date = new Date();
anotherToday.setMonth(anotherToday.getMonth() - 6);
var sixMonthsInThePast:Date = anotherToday;
trace(sixMonthsInTheFuture);
trace(sixMonthsInThePast);
For me, this returns
Mon May 28 14:03:32 GMT+0530 2012
Sat May 28 14:03:32 GMT+0530 2011
Which seems fair enough, right?

Um... var
var now:Date = new Date();
now.setMonth(now.getMonth() + 6);
var sixMonthsInTheFuture:Date = now;
var now:Date = new Date();
now.setMonth(now.getMonth() - 6);
var sixMonthsInThePast:Date = now;
Not sure if this would work 100%, as I'm not sure if Flex does date overflowing, (Like, would September + 6 be January the next year?), some languages do. Try it out
Apparently setMonth also takes a second paramater, Day, which is day number between 1 and 31, so some more ifs to check date and if it's more than the largest day of the new month etc., but this is the general idea

Related

Google Apps Script add some weird time

When I try to get time form table it plus 29 mins and 43 secs constantly
function onEdit(e){
var cal = CalendarApp.getCalendarById("h8ueab6ooel3iv8c7v4nd6bkvg#group.calendar.google.com");
Logger.log(cal)
// var arrayDate, arrayTimeStart, arrayTimeEnd;
var DateStart = new Date();
var DateEnd = new Date();
var table = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var dlinna = table.getLastRow();
var data = table.getRange("A2:E"+dlinna).getValues();
for (var i=data.length-1; i<data.length;i++){
Logger.log(data[i][2]);
}
}
[the given row]
[the weird time]

Working with Date, Time in Google Apps Script

I need help with some quick coding with google apps script, linking to my googlesheets spreadsheet.
In the googlespreadsheets, I have a cell with the value “26-Jun-2020”. It is a date.
I want to use google apps script to calculate the number of days difference between that date (“26-Jun-2020”) and today’s day, but it won’t do the calculation for me somehow.
If I print only “expiry_date[i]” using Logger.log(expiry_date[i]), it will provide the output “Fri Dec 17 2021 01:00:00 GMT-0500 (Eastern Standard Time) “
function Put_Options_Expiry_Alert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Long equity (sell puts)");
//var timeZone = AdsApp.currentAccount().getTimeZone(); //Get timezone of current spreadsheet
var status = sheet.getRange("F:F").getValues();
var expiry_date = sheet.getRange("M:M").getValues();
var potential_capitaloutlay_USD = sheet.getRange("Z:Z").getValues();
Logger.log("Length of status = " + status.length);
Logger.log("Length of expiry_date = " + expiry_date.length);
Logger.log("Length of potential_capitaloutlay_USD = " + potential_capitaloutlay_USD.length);
for (var i = 0; i < status.length; i++) {
if (status[i] == "Entered") { //Evaluate if this is a live Put position
//Calculate the time difference of two dates using date2. getTime() – date1. getTime();
//Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24)
Logger.log("expiry date is = "+expiry_date[i]);
Logger.log("today's date is = "+Date());
var days_to_expiry = dateDiffInDays(expiry_date[i],Date());
Logger.log(days_to_expiry);
}
}
}
// Function that returns the number of days difference between DateA and DateB
// DateA and DateB are javascript Date objects
function dateDiffInDays(DateA, DateB) {
var milliseconds_per_day = 1000 * 24 * 60; // number of milliseconds in a day
const utcA = Date.UTC(2021, DateA.getMonth(), DateA.getDate());
const utcB = Date.UTC(2020, DateB.getMonth(), DateB.getDate());
return Math.floor((utc2 - utc1) / milliseconds_per_day);
}
function timeDiffDays(Start, End) {
var day = 86400000;
var t1 = new Date(Start).valueOf();
var t2 = new Date(End).valueOf();
var d = t2 - t1;
return Math.floor(d / day);
}

Get Last Month Date In Flutter / Dart

in flutter we can get current month using this
var now = new DateTime.now();
var formatter = new DateFormat('MM');
String month = formatter.format(now);
But how to get the last month date? Especially if current date is January (01). we can't get the right month when we use operand minus (-) , like month - 1.
You can just use
var prevMonth = new DateTime(date.year, date.month - 1, date.day);
with
var date = new DateTime(2018, 1, 13);
you get
2017-12-13
It's usually a good idea to convert to UTC and then back to local date/time before doing date calculations to avoid issues with daylight saving and time zones.
We can calculate both first day of the month and the last day of the month:
DateTime firstDayCurrentMonth = DateTime.utc(DateTime.now().year, DateTime.now().month, 1);
DateTime lastDayCurrentMonth = DateTime.utc(DateTime.now().year, DateTime.now().month + 1).subtract(Duration(days: 1));
DateTime.utc takes in integer values as parameters: int year, int month, int day and so on.
Try this package, Jiffy, it used momentjs syntax. See below
Jiffy().subtract(months: 1);
Where Jiffy() returns date now. You can also do the following, the same result
var now = DateTime.now();
Jiffy(now).subtract(months: 1);
We can use the subtract method to get past month date.
DateTime pastMonth = DateTime.now().subtract(Duration(days: 30));
Dates are pretty hard to calculate. There is an open proposal to add support for adding years and months here https://github.com/dart-lang/sdk/issues/27245.
There is a semantic problem with adding months and years in that "a
month" and "a year" isn't a specific amount of time. Years vary by one
day, months by up to three days. Adding "one month" to the 30th of
January is ambiguous. We can do it, we just have to pick some
arbitrary day between the 27th of February and the 2nd of March.
That's why we haven't added month and year to Duration - they do not
describe durations.
You can use the below code to add months in a arbitrary fashion (I presume its not completely accurate. Taken from the issue)
const _daysInMonth = const [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
bool isLeapYear(int value) =>
value % 400 == 0 || (value % 4 == 0 && value % 100 != 0);
int daysInMonth(int year, int month) {
var result = _daysInMonth[month];
if (month == 2 && isLeapYear(year)) result++;
return result;
}
DateTime addMonths(DateTime dt, int value) {
var r = value % 12;
var q = (value - r) ~/ 12;
var newYear = dt.year + q;
var newMonth = dt.month + r;
if (newMonth > 12) {
newYear++;
newMonth -= 12;
}
var newDay = min(dt.day, daysInMonth(newYear, newMonth));
if (dt.isUtc) {
return new DateTime.utc(
newYear,
newMonth,
newDay,
dt.hour,
dt.minute,
dt.second,
dt.millisecond,
dt.microsecond);
} else {
return new DateTime(
newYear,
newMonth,
newDay,
dt.hour,
dt.minute,
dt.second,
dt.millisecond,
dt.microsecond);
}
}
To get a set starting point at the start of a month, you can use DateTime along with the Jiffy package.
DateTime firstOfPreviousMonth
= DateTime.parse(
Jiffy().startOf(Units.MONTH)
.subtract(months: 1)
.format('yyyy-MM-dd'). //--> Jan 1 '2021-01-01 00:00:00.000'
);
var fifthOfMonth
= firstOfPreviousMonth.add(Duration(days: 4)); //--> Jan 5 '2021-01-05 00:00:00.000'
or
DateTime endOfPreviousMonth
= DateTime.parse(
Jiffy().endOf(Units.MONTH)
.subtract(months: 2)
.format('yyyy-MM-dd'). //--> Dec 30 '2020-12-31 00:00:00.000'
// endOf always goes to 30th
);
var previousMonth
= endOfPreviousMonth.add(Duration(days: 2)); //--> Jan 1 '2021-01-01 00:00:00.000'
DateFormat('MMMM yyyy')
.format(DateTime(DateTime.now().year, DateTime.now().month - 2)),
List<DateTime> newList = [];
DateFormat format = DateFormat("yyyy-MM-dd");
for (var i = 0; i < recents.length; i++) {
newList.add(format.parse(recents[i]['date'].toString()));
}
newList.sort(((a, b) => a.compareTo(b)));
var total = 0;
for (var i = 0; i < newList.length; i++) {
if (DateTime.now().difference(newList[i]).inDays < 30) {
print(newList[i]);
total++;
}
}
print(total);
You can use this to fetch the last 30 days.
In addition to Günter Zöchbauer Answer
var now = new DateTime.now();
String g = ('${now.year}/ ${now.month}/ ${now.day}');
print(g);

How to Subtract Days From Date in TypeScript

I would like to subtract days from the current date in TypeScript.
For example, if the current date is October 1st, 2017, I would like to subtract 1 day to get September 30th, 2017, or if I want to subtract 3 days I would get September 28th etc.
This is what I have so far, the result is I received December 31st, 1969. Which I assume means that tempDate.getDate() is returning zero, as in the Epoch of January 1, 1970.
This is my code, the goal is to return the previous working day.
protected generateLastWorkingDay(): Date {
var tempDate = new Date(Date.now());
var day = tempDate.getDay();
//** if Monday, return Friday
if (day == 1) {
tempDate = new Date(tempDate.getDate() - 3);
} else if (1 < day && day <= 6) {
tempDate = new Date(tempDate.getDate() - 1);
}
return tempDate;
}
getDate returns the date of the month (1-31), so creating a new Date from it treats that number as "milliseconds since epoch".
What you probably want is to use setDate to change the date as it automatically handled going backwards through months/years.
protected generateLastWorkingDay(): Date {
const lastWorkingDay = new Date();
while(!this.isWorkingDay(lastWorkingDay)) {
lastWorkingDay.setDate(lastWorkingDay.getDate()-1);
}
return lastWorkingDay;
}
private isWorkingDay(date: Date) {
const day = date.getDay();
const isWeekday = (day > 0 && day < 6);
return isWeekday; // && !isPublicHoliday?
}
This is how I did
let yesterday=new Date(new Date().getTime() - (1 * 24 * 60 * 60 * 1000));
let last3days=new Date(new Date().getTime() - (3 * 24 * 60 * 60 * 1000));
We need to minus (no_of_days) * 24 * 60 * 60 * 1000 from current date.
You can just
const current = new Date()
and then
const numberOfDaysToSubstract= 3;
const prior = new Date().setDate(current.getDate) - numberOfDaysToSubstract);
you can see an example of this here
https://codepen.io/Jeysoon/pen/poNZRwd?editors=1112

How to minus the date from the current date in PL/SQL

I am new to SAP and XMI. I have a report that when the page loads it automatically gets the data for the current date. But i need the data to go a day backwards so for eg if today is the 29/06/2016 it shouldn't show any data for today but it should load for the 28/06/2016 which is -1 day back.
If i click on 27/06/2016 then it must minus 2 days back from the current day of the 29/06/2016.
So how do I use the current date to minus off the date that was inputed by the user so that PL/SQl knows how many days previously to go back?
function Start()
{
if (document.frmMain["DATEFROM"].value == '{DATEFROM}')
{
var MONTH_NAMES=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var SD = new Date();
var ED = new Date();
var dateInMs = SD.getTime();
SD.setTime(dateInMs);
var month = MONTH_NAMES[SD.getMonth()];
var day = SD.getDate();
var year = SD.getFullYear();
var hours = SD.getHours();
var minutes = SD.getMinutes();
document.frmMain["DATEFROM"].value = LZ(day) + "-" + month + "-" + year + " 23:59:59";
}
else {
} }
HTML
<tr>
<input name="dtpDATEFROM" type="button" id="dtpDATEFROM" onclick="MM_callJS('popUpCalendar(frmMain.dtpDATEFROM, frmMain.DATEFROM, \'dd-mmm-yyyy 00:00:00\')')" value="...."> </font></td>
</tr>
What was tried:
SELECT *
FROM table
WHERE TRUNC(Date_Production )=trunc(sysdate) to_date('2016/06/29','yyyy/mm/dd')
If you're just trying to subtract days from a given date you can just do "date - n":
select trunc(sysdate) today, trunc(sysdate)-1 yesterday, trunc(sysdate)-2 two_days_ago from dual;

Resources