Set current time in flex - actionscript-3

I am using DateField in my application.
<mx:DateField id="dtNewDate" selectedDate="{new Date()}" change="{dtChangeHandler(event)}" />
On change i am getting selected date. And time is 00:00.
Now, i need current time instead of 00. So, i tried.
public function dtChangeHandler(event:CalendarLayoutChangeEvent):void
{
var df:DateFormatter =new DateFormatter();
df.formatString="MM/DD/YYYY HH:NN:QQ";
trace(df.format(event.currentTarget.selectedDate))
}
It will give 24:00:00 instead of 00:00:00. Is there any way i can set current time?
I had also tried:
var date:Date = new Date();
selected_date = (event.currentTarget.selectedDate);
selected_date.setTime(date.getTime());
It will change time to current time. But, it will change date to today date.
Any help will greatly appreciated.

Just use the selected date and overwrite its time:
public function dtChangeHandler(event:CalendarLayoutChangeEvent):void
{
var selectedDate:Date = event.currentTarget.selectedDate;
var currentDate:Date = new Date();
selectedDate.hours = currentDate.hours;
selectedDate.minutes = currentDate.minutes;
selectedDate.seconds = currentDate.seconds;
trace(selectedDate);
}

Related

two different hours subtraction Angular 6

I am trying to do how to subtract two different hours that is captured:
Here is TS code
clockingIn() {
var dt = new Date()
this.clockedIn= new Date().getHours()+':'+ new Date().getMinutes()+':'+ new Date().getSeconds();
console.log('clockin', this.clockedIn);
this.disableClockIn = true;
this.disableClockOut = false;
}
clockingOut() {
var dt2 = new Date();
this.clockedOut= new Date().getHours()+':'+ new Date().getMinutes()+':'+ new Date().getSeconds();
console.log('clockout', this.clockedOut);
this.disableClockIn = false;
this.disableClockOut = true;
}
subtraction() {
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
}
//table
test() {
const data = {
dateClock: this.dateobj(),
Job: this.selectedJob,
clockI: this.clockedIn,
clockO: this.clockedOut,
Hoursworked: this.subtraction()
};
this.dataSource.data.push(data);
this.refresh();
console.log(this.dataSource);
}
So this is a clock in and Clock out.
I want these two can calculate (Clock Out - Clock In) to see the difference of two hours in the table that I made. So this way, I can see the diff time between Clock Out and Clock In.
I am assuming your duration will be calculated in subtraction function.
Below changes will get the result :
subtraction() {
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
// I have set to minutes. Change unit as per your requirement by
// referring doc given as reference
var unit = 'm';
return moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"), unit);
}
Moment.js duration

How can i compare a time string (hours:minutes) to the server date in AS3?

How can I compare a string that comes in this format:
var date1:String = "16:30";
to server clock in ActionScript 3?
Pretty simple. You just need to move the timestamp into a usable format using the split() method and then compare it to the local time using the Date class.
var time:String = "16:30";
var a:Array = time.split(":"); // == ["16", "30"]
var hours:Number = Number(a[0]);
var minutes:Number = Number(a[1]);
var curTime:Date = new Date();
var curHour:Number = curTime.hours; // 0-23 format. Adjust if needed
var curMinute:Number = curTime.minutes; //0-59 format. Adjust if needed
if (curHour > hours) {
// current time is greater than the timestamp
}
else if (curHour == hours && curMinute > minutes) {
// current time is greater than the timestamp
}
else if (curHour == hours && curMinute == minutes) {
// current time is equal to the timestamp
}
else {
// current time is less than the timestamp
}
You could obviously consolidate those conditions, but I did it this way to properly show the logic.
I'm unsure if there is a built in way to do this (as there is in other languages, like PHP), but I doubt it given the way AS3 handles time (there are no formatting options, no ways to add or subtract time, etc. Everything is expected to be handled by the programmer instead).

Run JQuery Function on Page Load and Store the Result

I've got a script that hides most of the page when you first load the webpage.
When it does this, I want it to run a function which gets the current date, then saves it.
I've having trouble getting to do this for a few reasons.
Here is the page load code, where it hides the page if it's not been setup:
$(document).ready(
function() {
if (setup=="true") {
$("#show-page").show();
loadSetup();
} else {
$("#page-nav").hide();
}
});
Here is the date function:
function getStartDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getYear();
var startDate = dd + (mm * 30) + (yyyy * 360)
//edit
// return startDate
$("#start-date").val(startDate);
localStorage.startDate = $("#start-date").val();
}
I then want an if function in the first part of the loading page which says if the stored start date + 30 is less than the current date, show a message that gives them an updated url (isNewUrlNeeded()).
This was my attempt:
$(document).ready(
function() {
if (setup=="true") {
$("#show-page").show();
loadSetup();
isNewUrlNeeded();
} else {
$("#page-nav").hide();
getStartDate();
}
});
The problem here is that the getStartDate value isn't stored and I'm not sure how to store it. The reason it needs to be stored is so I can call on it later in the function isNewUrlNeeded.
Any help would be greatly appreciated.
Thank you!
Kind Regards,
Gary
You can try this :
function getStartDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getYear();
var startDate = dd + (mm * 30) + (yyyy * 360)
$("#show-page").data("start-date", startDate);
}
To retrieve this date you could do the follow :
var startDate = $("#show-page").data("start-date");

How do I format this date string so that google scripts recognizes it?

I'm getting a json feed with a date string formatted like the following:
//2012-08-03T23:00:26-05:00
I had thought I could just pass this into a new Date as such
var dt = new Date("2012-08-03T23:00:26-05:00");
This works on jsfiddle but not in google scripts. It's returning an invalid date. After reading this post I recognize it may be how GAS interprets the date string so now I'm not sure how to reformat the date to make it work.
Is there a best way to reformat that date string so GAS can recognize it as a date?
Google Apps Script uses a particular version of JavaScript (ECMA-262 3rd Edition) and as you have discovered can't parse date/times in ISO 8601 format. Below is a modified function I use in a number of my Apps Scripts
var dt = new Date(getDateFromIso("2012-08-03T23:00:26-05:00"));
// http://delete.me.uk/2005/03/iso8601.html
function getDateFromIso(string) {
try{
var aDate = new Date();
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\\.([0-9]+))?)?" +
"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
var d = string.match(new RegExp(regexp));
var offset = 0;
var date = new Date(d[1], 0, 1);
if (d[3]) { date.setMonth(d[3] - 1); }
if (d[5]) { date.setDate(d[5]); }
if (d[7]) { date.setHours(d[7]); }
if (d[8]) { date.setMinutes(d[8]); }
if (d[10]) { date.setSeconds(d[10]); }
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
if (d[14]) {
offset = (Number(d[16]) * 60) + Number(d[17]);
offset *= ((d[15] == '-') ? 1 : -1);
}
offset -= date.getTimezoneOffset();
time = (Number(date) + (offset * 60 * 1000));
return aDate.setTime(Number(time));
} catch(e){
return;
}
}
Found this other possible and very simple code that seems to also do the job :
function test(){
Logger.log(isoToDate("2013-06-15T14:25:58Z"));
Logger.log(isoToDate("2012-08-03T23:00:26-05:00"));
Logger.log(isoToDate("2012-08-03T23:00:26+05:00"));
}
function isoToDate(dateStr){// argument = date string iso format
var str = dateStr.replace(/-/,'/').replace(/-/,'/').replace(/T/,' ').replace(/\+/,' \+').replace(/Z/,' +00');
return new Date(str);
}
The hard and sure shot way to make it work if the format is known beforehand is to parse it.
var dtString = "2012-08-03T23:00:26-05:00";
var date = dtString.split('T')[0];
var time = dtString.split('T')[1].split('-')[0];
var tz = dtString.split('T')[1].split('-')[1];
var dt = new Date(date.split('-')[0] , date.split('-')[1] - 1, // month is special
date.split('-')[2], time.split(':')[0],
time.split(':')[1], time.split(':')[2] , 0);
I haven't tested this exact piece of code, but have used similar code. So, this gives you a fair idea of how to proceed.
This worked for me, when converting date-string to date-object in Google Script.
The date-string was taken from the Google Sheet cell by getValues() method.
From: 01.01.2017 22:43:34 to: 2017/01/01 22:43:34 did the job. And then the new Date().
var dateTimeObj = new Date(stringDate.replace(/^(\d{1,2})[-.](\d{1,2})[-.](\d{4})/g,"$3/$2/$1"));
As of now new Date() seems to work:
var dT = new Date("2012-08-03T23:00:26-05:00");
console.info("dT: %s or %d", dT, dT.getTime());
returns dT: Sat Aug 04 06:00:26 GMT+02:00 2012 or 1.344052826E12 in Google Apps Script

Datefield Ranges - Flex

I have two Datefields .Is there a way that i can set the date of second Datefield to a specified range(say three days ...?. I tried to do it but not worked .. dont know to convert date back to string and format it .. here is the code
<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield1" change="leaveDate()" parseFunction="null" width="100" x="156" y="130"/>
<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield2" parseFunction="null" width="100" x="426" y="130"/>
private function leaveDate():void {
//var dateLeave:Date = dfield1.selectedDate;
var myTime:Date =dfield1.selectedDate;
dateAdd("date", 3, myTime);
//car formatter1:Datef
commentField.text+=myTime+"\n"
//dfield2.selectedDate = returnDate
}
public static function dateAdd(datepart:String = "", number:Number = 0, date:Date = null):Date
{
if (date == null) {
date = new Date();
}
var returnDate:Date = new Date(date.time);;
switch (datepart.toLowerCase()) {
case "fullyear":
case "month":
case "date":
case "hours":
case "minutes":
case "seconds":
case "milliseconds":
returnDate[datepart] += number;
break;
default:
/* Unknown date part, do nothing. */
break;
}
return returnDate;
}
It would be really grateful if somebody can help
To operate calculations on date, you must first convert them into numbers, i.e. dates in Epoch time or amount of seconds elapsed since january the first in 1970. Then, you can performa any operation on your date. For instance :
// Today
var today:Date = new Date();
// Tomorrow
var tomorrow:Date = new Date();
// How many seconds in a day ?
var secsInOneDay:Number = 60*60*24;
// Changes the tomorrow date to actually be tomorrow ^^
tomorrow.setTime(today.getTime() + secsInOneDay);
If you want to disable the dates prior to the date selected in dfield1 and the dates after three days after the date selected in dfield1 then try something like this:
<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield1" change="leaveDate()" parseFunction="null" width="100" x="156" y="130"/>
<mx:DateField disabledRanges="{[{rangeEnd: new Date(2011,05,31)}]}" id="dfield2" parseFunction="null" width="100" x="426" y="130"/>
private function leaveDate():void {
dfield2.disabledRanges = [ { rangeEnd:new Date(
dfield1.selectedDate.fullYear,
dfield1.selectedDate.month,
dfield1.selectedDate.date - 1 ) },
{ rangeStart:new Date(
dfield1.selectedDate.fullYear,
dfield1.selectedDate.month,
dfield1.selectedDate.date + 3 ) } ];
}