Qt three ComboBoxes for a date - qt

I have been looking around Google and Stackoverflow, but I have yet to find out if there is any simple solution to auto-filling three comboboxes to represent a correct date (like YYYY-MM-DD). I would presume it would be related to QCalendarWidget. Any ideas?
I want to be able to scroll through current time to dates from X years ago, it shouldn't have non-existant dates like February 29, 2011. Not sure if this is asking for too much.

Now i get what's your idea.
The answer is simple. Make three combo boxes: Day (1 - 31), Month (1 - 12) and Year (i.e. 1999 - 2012). Create "OK" button. No ultra-logic is needed.
After button being pressed just validate the date by creating QDate object with three numbers given by user and calling QDate::isValid(). If it isn't, create some warning prompt and ask user to change something in input.
The best way to validate the data entered by user is to override QDialog::done() method.
void Dialog::done(int r)
{
if(r == QDialog::Accepted) {
QDate date;
//Create QDate from comboboxes' values
...
if(!date.isValid()) {
//Some warning to user.
return;
}
}
QDialog::done(r);
}

int X = 2;
QDate date = QDate::currentDate(), lastDate = date.addYears(-X);
for(; date > lastDate; date = date.addDays(-1))
ui->comboBox->addItem(date.toString("yyyy-MM-dd"));

Related

Java8 Date and Time with Instant API

I get dynamic date from my request,
Instant duration = request.getStartDate(); // my input is 2020-03-01T00:00:01Z
for (int i = 0; i < 12; i++) {
duration = duration.with(TemporalAdjusters.firstDayOfNextMonth ());
}
Basically i wanted to get first day of every month for 12 months, i tried with above code but getting exception,
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
My output could be,
2020-04-01T00:00:01Z
2020-05-01T00:00:01Z
2020-06-01T00:00:01Z
:
:
2021-03-01T00:00:01Z
anything am missing here? tried with using plus()
duration.plus(30, ChronoUnit.DAYS);
I tried to convert instant to locateDate, then it prints fine, but for my required format i need to convert to Instant from localdate. I'm not seeing 1st day of next month.
2019-03-31T22:00:00Z
2019-04-30T22:00:00Z
2019-05-31T22:00:00Z
2019-06-30T22:00:00Z
2019-07-31T22:00:00Z
2019-08-31T22:00:00Z
2019-09-30T22:00:00Z
2019-10-31T23:00:00Z
2019-11-30T23:00:00Z
2019-12-31T23:00:00Z
any suggestion are appreciated. Thanks!
Much less fiddly than my namesake's answer: provided you convert to an OffsetDateTime up-front, there is no problem using the nice human-readable adjuster that you were trying to use initially.
OffsetDateTime duration = Instant.now().atOffset(ZoneOffset.UTC);
for (int i = 0; i < 12; i++) {
duration = duration.with(TemporalAdjusters.firstDayOfNextMonth());
}
The issue is that an Instant is an unambiguous point in time, irrespective of any specific timezone or geographic location. There is no specific date associated with an instant; what is Monday the 1st in one location may be Sunday the 31st in another, but it's still the same instant. That is why when trying to set a first day of the month, you get an exception.
If you convert to an OffsetDateTime, you are applying an offset (in this case UTC, so an offset of zero). This converts your data to a format in which a date is unambiguous.
Instant startInstant = request.getStartDate();
LocalDate start = startInstant.atZone(ZoneOffset.UTC).toLocalDate().withDayOfMonth(1);
for (int i = 0; i < 12; i++) {
System.out.println(start.plusMonths(i).atStartOfDay().toInstant(ZoneOffset.UTC));
}

Regex verification correct birth date and check age

I need a regex which takes the string YYYY-MM-DD-XXXX (The last 4 are just for purpose of gender/area) It's mostly important to check the first 8 Digits for a valid birth date.
So far i have this:
/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})\-([0-9]{4})$/
Also i want to check so the input age is at least 18 years old. Would appreciate if somone had some input on how to achieve this.
Edit: The regex above was tested in JS, but should work fine in ASP as well?
I have changed your regex a bit to make it look more authentic
^([1-2]\d{3})\-([0-1][1-9])\-([0-3][0-9])\-([0-9]{4})$
years like 3012 will not pass.
Now you want to find whether a person is 18 years or not.
One approach could be to find the difference between the years of dates provided like this
var str = '1990-09-12-5555';
var res = /^([1-2]\d{3})\-([0-1][1-9])\-([0-3][0-9])\-([0-9]{4})$/.exec(str);
var year_now = new Date().getFullYear();
console.log(year_now-res[1]);
a second approach will be more precise one :
var str = '1990-09-12-5555';
var res = /^([1-2]\d{3})\-([0-1][1-9])\-([0-3][0-9])\-([0-9]{4})$/.exec(str);
var todays_date = new Date();
var birth_date = new Date(res[1],res[2],res[3]);
console.log(todays_date-birth_date);
will output the result in milliseconds. You can do the math to convert it into year
Cheers , Hope that helps !
I suggest using moment.js which provides an easy to use method for doing this.
interactive demo
function validate(date){
var eighteenYearsAgo = moment().subtract("years", 18);
var birthday = moment(date);
if (!birthday.isValid()) {
return "invalid date";
}
else if (eighteenYearsAgo.isAfter(birthday)) {
return "okay, you're good";
}
else {
return "sorry, no";
}
}
To include moment in your page, you can use CDNJS:
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js"></script>
Source
The following will match any year with a valid day/month combination, but won't do validation such as checking you've not entered 31 days for February.
^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])\-[0-9]{4}$
Not sure exactly what you're trying to achieve but I'd suggest using a date library for this sort of thing. You could return a message to the user somehow if the entered date fails to parse into an object.
In order to do age validation, you will certainly need to use a library so a regex should only be used for date validation purposes

How to filter on report?

I am working on a report in AX 2009. I want to filter data of InventSiteID on the basis of ExpDate.
I have 2 datasource in the query which is attached to report. Both the data source are same InventExpired. I have to show 4 fields in dialog i.e. SiteID, Exp Date for datasource1 and same for datasource 2 and then filter it out.
In your report, you can use
SysQuery::findOrCreateRange(this.queryRun().query().dataSourceNo(1),
fieldNum(InventExpired, ExpDate)
).value(SysQuery::value(yourFilterDate));
That will filter the first datasource with the date entered.
If you need to filter by dates greater than or less than the filter date, you can use
SysQuery::findOrCreateRange(...).value('>' + SysQuery::value(yourFilterDate));
or
SysQuery::findOrCreateRange(...).value('<' + SysQuery::value(yourFilterDate));
Do you know how to add the fields to the dialog?
If you don't, you should override the dialog() method, and in the dialog() method, after the call to super(), you should use:
Dialog d = ret;
expDateField = d.addField(typeid(yourDateEDT), "Expiry Date");
To get the values from the fields and use them in your report, you should use
expDateField.value()
I haven't tested this, but I've done similar things on numerous occasions so I'm fairly confident this will work. Let me know if you have any problems with this
If you want to specify a range here is how I accomplished it. Hope this helps somebody...
Method1:
qbds3 = qry.dataSourceTable(tableNum(DMxVehicleTable));
SysQuery::findOrCreateRange(qbds, fieldNum(DMxVehicleTable,VehicleMSRPRetails)).value(strFmt('(VehicleMSRPRetails >= %1) && (VehicleMSRPRetails <= %2)', queryValue(VehicleMinPrice), queryValue(VehicleMaxPrice)));
Method2:
qbds4 = qry.dataSourceTable(tableNum(DMxSysYearTable));
SysQuery::findOrCreateRange(qbds4, fieldNum(DMxSysYearTable,Year), 1, 1).value('>' + SysQuery::value(VehicleModelYearStart));
qbds4.addRange(fieldNum(DMxSysYearTable, Year)).value(strFmt('< %1', queryValue(VehicleModelYearEnd)));

Is there a better way to encode a (MS) DateTime as an Int?

BACKGROUND (ok to ignore/skip):
I feel like there should be a better way to do what I'm doing, but I don't know what it is and I think my solution works, but I thought I'd ask in case there is something more elegant or faster or whatever.
I am developing a web page with MS Razor MVC which uses Html.DropDownList, which gives a pick list UI control which maps choices as display strings to integer ID codes. (I don't think I can have it map strings to DateTime values.)
But in a couple of cases, I want to choose from dates that are stored as DateTime objects. I realize I could make yet another table in memory that relates ID codes to DateTime values, but I thought instead (and also because I think I may want to encode dates as ints for yet another workaround of web page annoyances), I would encode the date as an int.
PROBLEM:
How best to convert the date of a DateTime object as an int value, and then later set a DateTime's date back to that date from the encoded int. The main ugliness of my solution is that DateTime provides a read-only DayOfYear function, but no way I know of to set a date back to (Date, DayOfYear), so I wrote a method with a loop, which is cute but probably slowish.
MY CURRENT (OK) SOLUTION:
public int encodeDate(DateTime date)
{
return ((date.Year - 2012) * 400) + date.DayOfYear;
}
public DateTime decodeDateCode(int dateCode)
{
int year = (dateCode / 400) + 2012;
int day = dateCode % 400;
// MS DateTime doesn't provide a direct reverse conversion for DayOfYear, so find it:
int month = 1;
int mThresh = DateTime.DaysInMonth(year, month);
while (day > mThresh)
{
day -= mThresh;
month++;
mThresh = DateTime.DaysInMonth(year, month);
}
DateTime dateValue = new DateTime(year, month, day);
return dateValue;
}
Ho about to format the timestamp as POSIX time (POSIX time / Unix time, see http://en.wikipedia.org/wiki/Unix_time)?
I had a similar problem myself and found a great solution here: Convert a Unix timestamp to a .NET DateTime
From the link:
DateTime ConvertFromUnixTimestamp(double timestamp)
{
}
double ConvertToUnixTimestamp(DateTime date)
{
}

how do i validate date input in dd-mon-yyyy format in asp.net regularExpressionValidator?

I really neeed dd-mon-yy format because my oracle database accepts dates in this format.
Can I validate like that in RegularExpressionValidator like that?
And also, do I have to convert the textbox value to oracle data time format, when using nhibernate?
thanks a lot for help;
try
Regx.ValidationExpression=#"^(([1-9])|(0[1-9])|(1[0-2]))\-((0[1-9])
|([1-31]))\-((\d{2})|(\d{4}))$";
or
you can try this custom validator instead
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
DateTime result;
if (!DateTime.TryParse(args.Value, out result))
{
args.IsValid = false;
return;
} else args.IsValid = true;
}
Is it necessary to force the user to enter the data in the format proscribed by the Oracle database? As long as the user enters a valid date, you can parse the input into a DateTime object and then call ToString("dd-MMMM-yy") to produce a representation of the date in the format required by the database. This gives you the freedom to provide more a user-friendly means of collecting input from the user e.g. a date/time picker type control.
For two digits for the year try:
^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][Bb]|[Mm][Aa][Rr]|[Aa][Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][Uu][Ll]|[Aa][Uu][Gg]|[Ss][Ee][Pp]|[Oo][Cc][Tt]|[Nn][Oo][Vv]|[Dd][Ee][Cc])-\d\d$
With four digits for the year:
^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][Bb]|[Mm][Aa][Rr]|[Aa][Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][Uu][Ll]|[Aa][Uu][Gg]|[Ss][Ee][Pp]|[Oo][Cc][Tt]|[Nn][Oo][Vv]|[Dd][Ee][Cc])-(19|20)\d\d$
Those expressions mean:
0 or 1 or 2 followed by a number, or 3 plus 0 or 1
next a dash
For the month it 'tries' all the combinations of JAN, JAn, Jan, jan, etc... for all the twelve months
next a dash
Finally it expects two digits for 'yyyy' version or 19 or 20 followed by two digits for 'yy' version

Resources