Your Shopping Cart

 x 

Cart empty
   

Share, Like or Comment on...  

primi sui motori con e-max.it
   

Member Login  

   

Members Online  

   
This article contains a short method on how to obtain the date / time functions utilizing different programming methods in C++. 

#include <cstdlib> #include <string> #include <iostream> #include <iomanip> #include <windows.h>
using namespace std; int main() { SYSTEMTIME sysTime; TCHAR szBuffer[128]; GetLocalTime(&sysTime); char timeMeridian[255]; char Month[30]; char dayOfWeek[30]; int dayHour, dayMinute; switch (sysTime.wDayOfWeek) { case 1: //cout << "Monday, "; strcpy(dayOfWeek, "Monday"); break; case 2: //cout << "Tuesday, "; strcpy(dayOfWeek, "Tuesday"); break; case 3: //cout << "Wednesday, "; strcpy(dayOfWeek, "Wednesday"); break; case 4: //cout << "Thursday, "; strcpy(dayOfWeek, "Thursday"); break; case 5: //cout << "Friday, "; strcpy(dayOfWeek, "Friday"); break; case 6: //cout << "Saturday, "; strcpy(dayOfWeek, "Saturday"); break; case 7: //cout << "Sunday, "; strcpy(dayOfWeek, "Sunday"); break; default: cout << "Cannot allocate day of week. Error " << endl; return -1; } switch (sysTime.wMonth) { case 1: //cout << "January"; strcpy(Month, "January"); break; case 2: // cout << "February"; strcpy(Month, "February"); break; case 3: // cout << "March"; strcpy(Month, "March"); break; case 4: // cout << "April"; strcpy(Month, "April"); break; case 5: // cout << "May"; strcpy(Month, "May"); break; case 6: // cout << "June"; strcpy(Month, "June"); break; case 7: // cout << "July"; strcpy(Month, "July"); break; case 8: // cout << "August"; strcpy(Month, "August"); break; case 9: // cout << "September"; strcpy(Month, "September"); break; case 10: // cout << "October"; strcpy(Month, "October"); break; case 11: // cout << "November"; strcpy(Month, "November"); break; case 12: // cout << "December"; strcpy(Month, "December"); break; default: cout << "There is an error. Something went wrong with the months." << endl; strcpy(Month, " <Err. Displaying Month "); return -1; } //RETURN MERIDIAN if (sysTime.wHour > 12) { strcpy(timeMeridian, "PM"); } else { //timeMeridian = "AM"; strcpy(timeMeridian, "AM"); } //GRAB THE HOUR/MINUTE dayHour = sysTime.wHour; dayMinute = sysTime.wMinute; //CHECK IF THE TIME FORMAT IS CORRECT if ( dayMinute < 10 ){ switch (dayMinute) { case 1: dayMinute = 01; break; case 2: dayMinute = 02; break; case 3: dayMinute = 03; break; case 4: dayMinute = 04; break; case 5: dayMinute = 05; break; case 6: dayMinute = 06; break; case 7: dayMinute = 07; break; case 8: dayMinute = 0x08; break; case 9: dayMinute = 0x09; break; default: cout << "No clue what is going on here!!!" << endl; break; } } //cout << "Today is: " << dayOfWeek << endl; cout << "Today is: " << dayOfWeek << " " << sysTime.wDay << " of " << Month << " " << sysTime.wYear << endl; cout << "United States Format : " << sysTime.wMonth << "/" << sysTime.wDay << "/" << sysTime.wYear << endl; cout << "European Date Format : " << sysTime.wDay << "/" << sysTime.wMonth << "/" << sysTime.wYear << endl; cout << "Military Time Format : " << dayHour << ":" << dayMinute << " " << timeMeridian << endl; cout << "12 Hour Time Format : "; if (dayHour >12) { cout << dayHour - 12 << ":" << dayMinute << " " << timeMeridian << endl; } return 0; }
   
© ALLROUNDER