Skip to content
View in the app

A better way to browse. Learn more.

Komuniti @PuTeRA

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Time And Date In C++

Featured Replies

assalamualaikum...aku perlukan bantuan dalam c++ programming untuk mengunakan time dan date
ini contoh code time dan date aku
[code]#include <iostream.h>
#include <time.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
cout<<"Current local time and date: "<<(asctime(timeinfo))<<endl;

return 0;
}[/code]

Output dia akan keluar mcm nie
Curren Local time and date : Mon May 17 22:44::03 2010

ape yg aku nak sekarang adalah mcm mana nak buat time dan date dipisahkan dan time tidak mengunakan sistem 24jam contoh
Time : 10:44:03
Date : Mon May 17 2010
harap anda sekalian dapat membantu saya...
[font="Tahoma"]penggunaan asctime tu kurang tepat. sebab asctime takleh buat benda ni.
kalau nak formatkan output tu guna strftime(). bawah ni contoh yang aku dah buat dalam Dev-C++.[/font]

[code]#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
time_t now;
struct tm *tm_now;
char buff[BUFSIZ];

now = time ( NULL );
tm_now = localtime ( &now );

strftime ( buff, sizeof buff, "%X", tm_now );
cout << ( "Time : ", buff ) << endl;

strftime ( buff, sizeof buff, "%b %a %d %Y", tm_now );
cout << ( "Date : ", buff ) << endl;

system("PAUSE");
return EXIT_SUCCESS;

}[/code]

[font="Tahoma"]benda yang penting adalah kat line strftime() tu.[/font]

[code]strftime(timeString, timeStringLength, timeStringFormat, curTime);[/code]
[font="Tahoma"]ada byk cara nak dapatkan timeString ngan timeStringLength, tapi kat atas tu salah satu cara. nak default set terus pun boleh.[/font]

ok lastly bawah ni format dia.
[code]%a /* Abbreviated weekday */
%A /* Full weekday */
%b /* Abbreviated month */
%b /* Full month */
%c /* Full date and time */
%d /* Day of the month (1-31) */
%H /* Hour (24 hour clock) */
%I /* Hour (12 hour clock) */
%j /* Day of the year (1-366)*/
%m /* Month (1-12) */
%M /* Minute (0-59) */
%p /* AM/PM for 12 hour clock */
%S /* Second (0-60) */
%U /* Week number from Sunday */
%w /* Weekday (0-6) from Sunday */
%W /* Week number from Monday */
%x /* Full date */
%X /* Full time of day */
%y /* Year without century */
%Y /* Year with century */
%Z /* Time zone */
%% /* Print a % character */[/code]

[font="Tahoma"]@credits to Cprogramming.com & bytes.com[/font]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.