2e.wxWidgets – Date and Time

OVERVIEW

wxWidgets provides powerful date and time management functions like formatting time strings, handling time zones and doing time calculations.

SAMPLE CODE

datetime.cpp
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif
#include <wx/string.h>
#include <wx/datetime.h>

int main(int argc, char **argv) {
	wxInitialize();

	wxDateTime now = wxDateTime::Now();
	wxString format1 = now.Format();
	wxString format2 = now.Format(wxT("%x"));
	wxString format3 = now.Format(wxT("%X"));
	wxString format4 = now.Format(wxT("%A %B"));

	wxPuts(format1);
	wxPuts(format2);
	wxPuts(format3);
	wxPuts(format4);

	wxString nyc =  now.Format(wxT("%a %T"), wxDateTime::EDT).c_str();
	wxPuts(wxT("New York:") + nyc);

	wxString tokyo =  now.Format(wxT("%a %T"), wxDateTime::GMT9).c_str();
	wxPuts(wxT("Tokyo:") + tokyo);

	wxDateSpan span1 (0,1,0);
	wxDateSpan span2 (1,6,0);
	wxDateTime date1 = now.Add(span1);
	wxPrintf(date1.Format(wxT("%B %d %Y\n")));

	wxDateTime date2 = now.Add(span2);
	wxPrintf(date2.Format(wxT("%B %d %Y\n")));

	wxTimeSpan timespan1 (1,30,0);
	wxDateTime time1 = wxDateTime::Now();
	time1.Add(timespan1);
	wxPrintf(time1.Format(wxT("%X\n")));

	wxUninitialize();
}

The output is given below:

Sat Dec 11 11:45:27 2021
 12/11/21
 11:45:27
 Saturday December
 New York:Sat 02:15:27
 Tokyo:Sat 15:15:27
 January 11 2022
 July 11 2023
 13:15:27

1 Trackback / Pingback

  1. 1.wxWidgets – Introduction – Truelogic Blog

Leave a Reply

Your email address will not be published.


*