Jump to content
Sign in to follow this  
bita

Time And Date In C++

Recommended Posts

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...

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites

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...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...