0

We can get datetime using qmake _DATE_ which outputs

Sat Mar 12 17:29:00 2022 

Can we format this output?

KcFnMi
  • 5,516
  • 10
  • 62
  • 136
  • You mean, the compilation date/time ? – Yoruk May 05 '22 at 05:49
  • Yes the compilation date/time – KcFnMi May 05 '22 at 06:00
  • I'm using this syntax in my .PRO file : (depending if you are in windows or not) win32 { DEFINES += BUILDTIME=\\\"$$system('echo %time%')\\\" DEFINES += BUILDDATE=\\\"$$system('echo %date%')\\\" } else { DEFINES += BUILDTIME=\\\"$$system(date '+%H:%M.%s')\\\" DEFINES += BUILDDATE=\\\"$$system(date '+%d/%m/%y')\\\" } then this in my code : QString::fromLocal8Bit(BUILDDATE) By using QdateTime you may be able to convert the date (fromString() into what you want... – Yoruk May 05 '22 at 06:05
  • See https://stackoverflow.com/questions/72122206/echo-date-en-vs-cn?noredirect=1#comment127432911_72122206 – KcFnMi May 05 '22 at 06:08
  • Does this answer your question? [Get the date with qmake](https://stackoverflow.com/questions/29753345/get-the-date-with-qmake) – JarMan May 05 '22 at 13:12

1 Answers1

0

By using QDateTime by passing the value of DATE (in fromString), you may be able to format the output the way you want, by using toString.

You can try things like this :

toString("ddd MMMM d  hh:mm:ss yy");  //Sat Mar 12 17:29:00 2022 
Yoruk
  • 103
  • 8
  • I need it at qmake time, as specified on question title. Because I'll write files at qmake time using datetime on filename. – KcFnMi May 05 '22 at 06:13
  • Aouch sorry, I got it. Check my first comment and https://stackoverflow.com/questions/29753345/get-the-date-with-qmake – Yoruk May 05 '22 at 06:15
  • Well I didn't got your point with the comment, compilation date/time is what I call qmake time, right? – KcFnMi May 05 '22 at 06:38
  • Yes ! In my case, BUILDTIME and BUILDDATE variables are containing the date/time when I actually run qmake. But watch out, you can actually compile your code without calling qmake, and have a physical compilation datetime different from the qmake one ! – Yoruk May 05 '22 at 06:55
  • Yes for the watch out case, not getting into it though. But it's explained in the question (comment below your comment) why that approach cannot be used, `%date%` depends on locate. Am I missing something? – KcFnMi May 05 '22 at 07:44
  • Yes, the location is a problem... There are solutions to format the output of %time% and %date% but it may hard to implement... See https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format – Yoruk May 05 '22 at 08:57
  • `wmic os get LocalDateTime` seems interesting – KcFnMi May 05 '22 at 13:32