Date in File Name: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
[[Category:HowTo]]<br />[[Category:snippets]]
'''English''' | [[Date-in-File-Name-Russian|Русский]]
'''English''' | [[Date-in-File-Name-Russian|Русский]]


=Date in File Name=
= 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(){<br /> QDate today = QDate::currentDate();<br /> QString relativeFileName( &quot;data_&quot; );<br /> relativeFileName.append( QString::number( today.month() ) );<br /> relativeFileName.append( &quot;''&quot; );<br /> relativeFileName.append( QString::number( today.day() ) );<br /> relativeFileName.append( &quot;''&quot; );<br /> relativeFileName.append( QString::number( today.year() ) );<br /> relativeFileName.append( &quot;.txt&amp;quot; );<br /> return relativeFileName;<br />}</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:


The fastest and clearest version, without calling a custom function, is
<code>QString FileNameHandler::todayFileName(){<br /> QDate today = QDate::currentDate();<br /> QString relativeFileName( &quot;data_&quot; );<br /> relativeFileName + QString::number( today.month()<br /> + &quot;''&quot; + QString::number( today.day() )<br /> + &quot;''&quot; + QString::number( today.year() )<br /> + &quot;.txt&amp;quot;;
 
return relativeFileName;<br />}</code>
 
The fastest - and clearest - version, without calling a custom function, is
 
<code>QString filename = QDate::currentDate().toString(&quot;'data_'MM_dd_yyyy'.txt'&quot;);<code>


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


===Categories:===
</code>QString filename = QDate::currentDate().toString(&quot;'data_'yyyy_MM_dd'.txt'&quot;);</code>
 
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]

Revision as of 09:54, 24 February 2015


English | Русский

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.

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

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

QString FileNameHandler::todayFileName(){<br /> QDate today = QDate::currentDate();<br /> QString relativeFileName( &quot;data_&quot; );<br /> relativeFileName + QString::number( today.month()<br /> + &quot;''&quot; + QString::number( today.day() )<br /> + &quot;''&quot; + QString::number( today.year() )<br /> + &quot;.txt&amp;quot;;

return relativeFileName;<br />}

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

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

and when the files should get ordered chronological, then use

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