Adobe flex dateField - apache-flex

I have some code as follows:
private function onComboChange(evt:Event):void {
var temp:Date = df_date.selectedDate;
temp.date += 5;
df_dateDue.selectedDate = new Date(temp);
}
In essence, I am trying to add 5 days onto the selected date in df_date, and put that date into df_dateDue. This fires off via an EventListener on a combobox. Both df_date and df_dateDue are dateFields.
OK, so the first time that I run this, it works fine; df_date stays the same and df_dateDue is set to 5 days past df_date. However, the next time that I run it, df_dateDue increments by 10 days from df_date, the next time by 15, and so on.
So, stepping through the code shows that somehow df_date has become linked to the temp var, and that the temp var is not resetting itself each time the function is called.
Example: df_date = 01 Jan, df_dateDue = 01 Jan.
Fire off the event, df_date = 01 Jan, df_dateDue = 06 Jan
Fire off the event again. At this point, var temp = 06 Jan (even though df_date still shows 01 Jan), and df_dateDue is then set to 11 Jan
Fire off the event again. At this point var temp = 11 Jan (even though df_date = 01 Jan), and df_dateDue is then set to 16 Jan
What am I missing here?

In Flex/AS, variables that contain objects are really just pointers to some memory space. Date's in Flex are an Object, not a native type. This line:
var temp:Date = df_date.selectedDate;
Creates a new pointer to an existing date object. It does not create a copy.
This line:
temp.date += 5;
increments the date property of the dateObject. All references pointing to that date object will be updated. Try using objectUtil.copy
var temp:Date = ObjectUtil.copy(df_date.selectedDate) as Date;
Oh, and get your acceptance rate up.

Related

What does NNN mean in date format <YYMMDDhhmmssNNN><C|D|G|H>?

hi I has date format and I want converted to correct GMT date :
<YYMMDDhhmmssNNN><C|D|G|H>
Sample value on that date:
210204215026000C
I get this explanation for part NNN :
NNN If flag is C or D then NNN is the number of hours relativeto GMT,
if flag is G or H, NNN is the number of quarter hours relative to GMT
C|D|G|H C and G = Ahead of GMT, D and H = Behind GMT
but I did not get how number of hours relative to GMT can present on 3 digits ? it should be in 2 digit as i knew the offset for hours related to GMT is from 0 to 23 , and also what quarter hours relative to GMT mean ?
I want to use Scala or Java.
I don’t know why they set 3 digits aside for the offset. I agree with you that 2 digits suffice for all cases. Maybe they just wanted to be very sure they would never run of out space, and maybe they even overdid this a bit. 3 digits is not a problem as long as the actual values are within the range that java.time.ZoneOffset can handle, +/-18 hours. In your example NNN is 000, so 0 hours from GMT, which certainly is OK and trivial to handle.
A quarter hour is a quarter of an hour. As Salman A mentioned in a comment, 22 quarter hours ahead of Greenwich means an offset of +05:30, currently used in Sri Lanka and India. If the producer of the string wants to use this option, they can give numbers up to 72 (still comfortably within 2 digits). 18 * 4 = 72, so 18 hours equals 72 quarter hours. To imagine a situation where 2 digits would be too little, think an offset of 25 hours. I wouldn’t think it realistic, on the other hand no one can guarantee that it will never happen.
Java solution: how to parse and convert to GMT time
I am using these constants:
private static final Pattern DATE_PATTERN
= Pattern.compile("(\\d{12})(\\d{3})(\\w)");
private static final DateTimeFormatter FORMATTER
= DateTimeFormatter.ofPattern("uuMMddHHmmss");
private static final int SECONDS_IN_A_QUARTER_HOUR
= Math.toIntExact(Duration.ofHours(1).dividedBy(4).getSeconds());
Parse and convert like this:
String sampleValue = "210204215026000C";
Matcher matcher = DATE_PATTERN.matcher(sampleValue);
if (matcher.matches()) {
LocalDateTime ldt = LocalDateTime.parse(matcher.group(1), FORMATTER);
int offsetAmount = Integer.parseInt(matcher.group(2));
char flag = matcher.group(3).charAt(0);
// offset amount denotes either hours or quarter hours
boolean quarterHours = flag == 'G' || flag == 'H';
boolean negative = flag == 'D' || flag == 'H';
if (negative) {
offsetAmount = -offsetAmount;
}
ZoneOffset offset = quarterHours
? ZoneOffset.ofTotalSeconds(offsetAmount * SECONDS_IN_A_QUARTER_HOUR)
: ZoneOffset.ofHours(offsetAmount);
OffsetDateTime dateTime = ldt.atOffset(offset);
OffsetDateTime gmtDateTime = dateTime.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println("GMT time: " + gmtDateTime);
}
else {
System.out.println("Invalid value: " + sampleValue);
}
Output is:
GMT time: 2021-02-04T21:50:26Z
I think my code covers all valid cases. You will probably want to validate that the flag is indeed C, D, G or H, and also handle the potential DateTimeException and NumberFormatException from the parsing and creating the ZoneOffset (NumberFormatException should not happen).

How do I show time in ASP.NET?

I have a label in my asp.net web site that will shows the time. I want the output like here. in the morning like this: 08:26 and after 12 am,it shows 15:28
My code does not work. It only supports the first part.
DateTime tim = DateTime.Now;
int hh = p.GetHour(tim);
int mm = p.GetMinute(tim);
Label7.Text = DateTime.Now.ToString("hh:mm");
According to the Custom date and time format strings docs page - you can see:
"hh" The hour, using a 12-hour clock from 01 to 12.
"HH" The hour, using a 24-hour clock from 00 to 23.
So in your case - just use the capitalized HH for your formatting:
Label7.Text = DateTime.Now.ToString("HH:mm");
and you should get what you're looking for.

Issue with date formatting and time zone change

I am having a weird issue with dates formatting in Google Sheets and Google Script.
I am developing a script that reads a list of dates in a Google sheet and then sends automatic emails to a list of recipients. I have a cell in the spreadsheet with a date formatted as "dd/mm/yyyy" and then I use the following formulas (cell + 7, cell + 14, cell + 21, cell + 28, etc...) to generate all the other dates in the spreadsheet.
I use the following code to read the dates in the spreadsheet:
var sheet = SpreadsheetApp.getActiveSheet();
var drng = sheet.getDataRange();
var rng = sheet.getRange(4, 2, drng.getLastRow()-3, drng.getLastColumn());
var rngData = rng.getValues();
var todayDate = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
// CYCLE THROUGH DATA
for (var i = 0; i < rngData.length; i++) { // cycle through the rows in the spreadsheet
var ptLastName = rngData[i][0]; // fixed patient data used to email customization
var ptFirstName = rngData[i][1];
var ptDoB = Utilities.formatDate(rngData[i][2], "GMT+1", "dd/MM/yyyy");
var ptSex = rngData[i][3];
var ptEmail = rngData[i][4];
var ptDoS = Utilities.formatDate(rngData[i][5], "GMT+1", "dd/MM/yyyy");
var ptDoD = Utilities.formatDate(rngData[i][6], "GMT+1", "dd/MM/yyyy");
for (var j = 7; j <= 14; j++) { // cycle through follow-up data for the current patient
var currentDate = Utilities.formatDate(rngData[i][j], "GMT+1", "dd/MM/yyyy");
if (currentDate == todayDate) { // send email with the questionnaire
writeEmail(ptFirstName, ptLastName, ptSex, ptDoD, ptEmail);
rng.getCell(i+1, j+1).setBackground("yellow");
}
}
}
The issue is that when I try for read and format some of the dates they are not corresponding to the ones listed in the spreadsheet. I have noted that the wrong dates in the code are systematically 1 day earlier than the ones listed in the spreadsheet. On a closer look I have noted that some of the dates read from the spreadsheet at GMT+2 while others are GMT+1.
That's weird because I have checked my system settings (it GMT+1) as well as the spreadsheet settings (GMT+1). So I don't understand why some dates are automatically converted to GMT+2.
I tried it this way and it works okay:
function dattest() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const rg=sh.getDataRange();
const vs=rg.getValues()[0];
vs.forEach(function(d){
Logger.log(Utilities.formatDate(new Date(d),Session.getScriptTimeZone(),"dd/MM/yyyy"));
});
}
Display Values:
8/1/2020,8/8/2020,8/15/2020,8/22/2020
Formulas:
,=A1+7,=A1+14,=a1+21
The output from view Executions:
Aug 1, 2020, 2:52:37 PM Info 01/08/2020
Aug 1, 2020, 2:52:37 PM Info 08/08/2020
Aug 1, 2020, 2:52:37 PM Info 15/08/2020
Aug 1, 2020, 2:52:37 PM Info 22/08/2020
Seems correct to me
thank you for your help.
I finally found out what the problem was.
The dates in the spreadsheet were stored as dd/mm/yyyy, and the system automatically added 00.00.00 as time to them. As a result, when I was trying to read some of the dates with the code some of them were converted as the day before because of the daylight saving time adjustment. I have managed to solve the issue adding 1/3 of a day to all the dates. This way they are stored as dd/mm/yyyy 8.00.000 am and they remain the same day all year long.

Getting the month number by month name with Moment.js

I am trying to return the month number passing the month name using MomentJS. For example if I pass "July" to moment() I would expect 7 to be returned.
After reading through the docs I tried several different ways, and this way came close...
console.log(moment().month("July"));
In the console, buried in the response I could see this...
_monthsParse: Array[7]
Could anyone please tell me how to return the month number using MomentJS correctly?
Try :
moment().month("July").format("M");
Relevant documentation: http://momentjs.com/docs/#/get-set/month/
alert(moment().month("July").format("M"));
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Anybody looking to get month name from month number then you can try :
const number = 1; // 0 = Jan & 11 = Dec
moment().month(number).format("MMM"); // Feb
Use following to get full month name :
const number = 1; // 0 = January & 11 = December
moment().month(number).format("MMMM"); // February
To use simple month number try this:
const month = 2 //Feb
moment(month, 'M').format('MMMM');
##get month name in moment js with node js
moment() give today date
format("DD-MMMM-YYYY") / output 18-May-2020
format("DD-MM-YYYY") / output 18-05-2020
- sperator you can use /
```
var moment = require('moment');
m_date = moment().format("DD-MMMM-YYYY");
console.log("moment date :", m_date)
```
##output
```
moment date : 18-May-2020
```
Read Officail Docdescription here

How do I work out how many days ago date1 was compared to date2, using datejs?

var d1 = Date.parse(date); // eg, 7th jan
var d2 = Date.today(); // eg, 2nd jan
I want to do:
(d2 - d1).days
which with the above data, should return 5.
Is this possible with datejs?
The optional TimeSpan module includes functionality to easily get various value differences between two date objects.
http://code.google.com/p/datejs/source/browse/trunk/src/time.js
Just include after your main date.js include, or combine.
In the "time.js" file, there's also a TimePeriod class. You might not require, so just crop what you need if weight is an issue.
The following sample demonstrates the full scenario.
Example
var d1 = Date.parse("jan 7"); // eg, 7th jan
var d2 = Date.parse("jan 2"); // eg, 2nd jan
console.log(new TimeSpan(d1 - d2).days); // 5
Hope this helps.

Resources