Date in File Name: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
m (Wieland moved page Date-in-File-Name to Date in File Name: underscores)
(Cleanup)
 
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:snippets]]
[[Category:snippets]]
'''English''' | [[Date-in-File-Name-Russian|Русский]]
= Date in File Name =
In the case you have to build a file name after the current date, so that it will look like ''data_mm_dd_yyyy.txt'' you can use the following simple function to get the string name of the file.
In the case you have to build a file name after the current date, so that it will look like ''data_mm_dd_yyyy.txt'' you can use the following simple function to get the string name of the file.


<code>QString FileNameHandler::todayFileName(){
<code>
QDate today = QDate::currentDate();
QString FileNameHandler::todayFileName() {
QString relativeFileName( "data_" );
    QDate today = QDate::currentDate();
relativeFileName.append( QString::number( today.month() ) );
    QString relativeFileName( "data_" );
relativeFileName.append( "''" );
    relativeFileName.append( QString::number( today.month() ) );
relativeFileName.append( QString::number( today.day() ) );
    relativeFileName.append( "''" );
relativeFileName.append( "''" );
    relativeFileName.append( QString::number( today.day() ) );
relativeFileName.append( QString::number( today.year() ) );
    relativeFileName.append( "''" );
relativeFileName.append( ".txt" );
    relativeFileName.append( QString::number( today.year() ) );
return relativeFileName;
    relativeFileName.append( ".txt" );
}</code>
    return relativeFileName;
}
</code>


Or its shorter version that uses the overloading of the + operator for QString:
Or its shorter version that uses the overloading of the + operator for QString:


<code>QString FileNameHandler::todayFileName(){
<code>
QDate today = QDate::currentDate();
QString FileNameHandler::todayFileName() {
QString relativeFileName( "data_" );
    QDate today = QDate::currentDate();
relativeFileName + QString::number( today.month()
    QString relativeFileName( "data_" );
+ "''" + QString::number( today.day() )
    relativeFileName + QString::number( today.month()
+ "''" + QString::number( today.year() )
        + "''" + QString::number( today.day() )
+ ".txt";
        + "''" + QString::number( today.year() )
 
        + ".txt";
return relativeFileName;
    return relativeFileName;
}</code>
}
</code>


The fastest - and clearest - version, without calling a custom function, is
The fastest - and clearest - version, without calling a custom function, is


<code>QString filename = QDate::currentDate().toString("'data_'MM_dd_yyyy'.txt'");<code>
<code>
QString filename = QDate::currentDate().toString("'data_'MM_dd_yyyy'.txt'");
</code>


and when the files should get ordered chronological, then use
and when the files should get ordered chronological, then use


</code>QString filename = QDate::currentDate().toString("'data_'yyyy_MM_dd'.txt'");</code>
<code>
QString filename = QDate::currentDate().toString("'data_'yyyy_MM_dd'.txt'");
</code>

Latest revision as of 13:39, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

In the case you have to build a file name after the current date, so that it will look like data_mm_dd_yyyy.txt you can use the following simple function to get the string name of the file.

QString FileNameHandler::todayFileName() {
    QDate today = QDate::currentDate();
    QString relativeFileName( "data_" );
    relativeFileName.append( QString::number( today.month() ) );
    relativeFileName.append( "''" );
    relativeFileName.append( QString::number( today.day() ) );
    relativeFileName.append( "''" );
    relativeFileName.append( QString::number( today.year() ) );
    relativeFileName.append( ".txt" );
    return relativeFileName;
}

Or its shorter version that uses the overloading of the + operator for QString:

QString FileNameHandler::todayFileName() {
    QDate today = QDate::currentDate();
    QString relativeFileName( "data_" );
    relativeFileName + QString::number( today.month()
        + "''" + QString::number( today.day() )
        + "''" + QString::number( today.year() )
        + ".txt";
    return relativeFileName;
}

The fastest - and clearest - version, without calling a custom function, is

QString filename = QDate::currentDate().toString("'data_'MM_dd_yyyy'.txt'");

and when the files should get ordered chronological, then use

QString filename = QDate::currentDate().toString("'data_'yyyy_MM_dd'.txt'");