How to put a current date in QLabel in digits? - c++

I have a problem with convert a current date.
I want to put a current date in a QLabel but i can't put a QDate type.
QDate today;
today=QDate::currentDate();
datamiasto->setText("Data wystawienia: "+today+", xyz");
I can put it when i convert it to string but when i do it date is not in digits.
How can i put a digit date in a QLabel ?

Try this:
QDateTime dateTime = dateTime.currentDateTime();
QString dateTimeString = dateTime.toString("yyyy-MM-dd_hh-mm-ss");
QDateTime::toString(const QString &format) : Returns the datetime as a string. The format parameter determines the format of the result string. (give your custom format to the function)
QDateTime::currentDateTime() : Returns the current datetime, as reported by the system clock, in the local time zone.

Related

How to get date (day/month/year) from MonthCalendar in C++ Builder 6?

I'm creating an age counter app, but I couldn't use the date, which is user chose from the calendar. How can I use the day/month/year specified in MonthCalendar in my program?
TMonthCalendar has a Date property, which returns the user's selected date as a TDateTime value. You can extract the individual month, day, and year values from that, if needed, by using the TDateTime::DecodeDate() method.
TDateTime dtSelected = MonthCalendar1->Date;
Word wYear, wMonth, wDay;
dtSelected.DecodeDate(&wYear, &wMonth, &wDay);
...

How to sort and change date format in QTableView

I've already implemented a QTableView + QStandardItemModel in Qt5. At the beginning, I set the date data just as a string with the date format based on the application setting. For instance, it can be the US format like MM/dd/yyyy or the european format dd.MM.yyyy. The data comes from a json file with the european date format. My first implementation was like this:
shared_ptr<QStandardItemModel> _model;
// detect a date string with regex, get the submatches and create a QDate object from it
QDate date(stoi(submatches[3].str()), stoi(submatches[2].str()), stoi(submatches[1].str()));
QModelIndex index = _model->index(rowPos, colPos, QModelIndex());
// depends on the setting, the date can be shown on the table like this
_model->setData(index, QString(date.toString("dd.MM.yyyy"));
// activate the column sorting in the QTableView
ui->tableView->setSortingEnabled(true);
This implementation, however, cannot sort the date column correctly. The reason is because the QTableView sorts the column just like a string (sorted by day isntead of by year first) instead of a date entry.
I can change the implementation by setting the data directly with the date object:
_model->setData(index, date);
The sorting works perfectly by date. But, the format is now always shown in dd/MM/yyyy format.
How can I keep this sorting function, but change the date view depends on the date format setting?
I've read that it may be implemented using a custom subclass of QAbstractTableModel. How about implementing as a SubClass of QTableView? Or may be with a subclass of QAbstractItemModel like in here? I'm not an expert yet to implement and integrate a Qt5 subclass.
The solution is to pass the QDate as data to the model, and use a delegate to set as shown in the view:
_model->setData(index, date);
class DateDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QString displayText(const QVariant &value, const QLocale &locale) const{
return locale.toString(value.toDate(), "dd.MM.yyyy");
}
};
ui->tableView->setItemDelegateForColumn(col_date, new DateDelegate);

How to convert Particular timezone of date into GMT timezone using esql of MQ?

I have the date of particular timezone, and I want to convert it to the GMT timezone, and then it needs to be inserted into DB using esql of MQ. Please help to resolve this issue.
If you want to convert a date from a format to another, you can do the following :
DECLARE inDate DATE;
DECLARE outDate DATE;
DECLARE tempDate DATE;
DECLARE patternIN CHARACTER 'yyyy-MM-dd';
DECLARE patternOUT CHARACTER 'yyMMdd';
SET tempDate = CAST(inDate AS DATE FORMAT patternIN);
-- Convert input String as Date (should match patternIN)
SET outDate = CAST(tempDate AS CHARACTER FORMAT patternOUT)
-- Convert the date object to the desired date format
Of course you need to be able to define your date pattern. I know you might need to separate the DATE from the TIME, but the object are exactly the same. A quick example of a specific cast :
CAST(CURRENT_DATE AS CHARACTER FORMAT 'yyyy-MM-dd') || 'T' || CAST(CURRENT_TIME AS CHARACTER FORMAT 'HH:mm:SS')
This will generate a date in the XML format, e.g : 2019-08-28T16:46:32

How to create a new column converting date to text

I need to create a new column with an IF.
If the difference between two dates is more than a month I have to use a text-like "much time" but if it is not I have to show a date.
So the date must be converted to a string to use a text column.
How can I convert date to text?
Fecha_real =
IF( DATEDIFF(ventas[fecha_pedido]; ventas[fecha]; month) = 1 ;
"much time";
ConvertToTextInSomeWay ventas[fecha]
)
This is pretty simple with the FORMAT function.. For example, FORMAT(ventas[fecha], "Short Date") will convert fecha into textlike "12/31/2018".
That's just one format example. There are plenty of pre-defined and custom options if you'd rather something else. For example, FORMAT(ventas[fecha], "dd-mm-yyyy") would format that same date as "31-12-2018" instead.

How to convert QString to QDate in specific format?

I have a QDateEdit in my GUI from which I convert the QDate to QString and add it to my database. The QString date is saved in the database in this format: 20/12/2015.
In case a user want to edit the date, then I need to show the date on the QDateEdit field on the GUI again. Hence, I need to fetch the database, bring back the date (which is in QString format) and convert it to QDate back again in order to put it on the QDateEdit field on the GUI.
However, I cannot manage to convert that QString format (i.e.: 20/12/2015) to QDate using the following:
QString date_string_on_db = "20/12/2015";
QDate Date;
Date.fromString(date_string_on_db,"dd/MM/YYYY");
The Date is always returning invalid.
what should I do ?
First of all, the format string should be dd/MM/yyyy. Qt documentation for the QDate class says that yyyy is recognized as a four digit year number.
Second of all, fromString is a static function that returns a new QDate. Currently, the return value of that function is discarded : it is not written back into the Date variable, as you might think. The complete correct code should therefore look like this :
QString date_string_on_db = "20/12/2015";
QDate Date = QDate::fromString(date_string_on_db,"dd/MM/yyyy");