Results 1 to 3 of 3

Thread: month and year

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    UK
    Posts
    205

    month and year

    how can i get the current month and year as int values using C programming methods as opposed to C++.

    I cant use C++ because it is for a procedural programming assignment, not OO. I want to write them to my struct:
    Code:
    struct temp_rec
    {
    	int iYear;
    	int iMonth;
    	int iTemp;
    	int iLoc;
    	char* sLoc;
    };
    
    temp_rec trRec;
    
    void get_temp()
    {
    	temp_rec* pRec = &trRec;
    	char sVal[20];
    	int iVal = 0;
    
    	do{
    		clrscr();
    		printf("Please enter the location: ");
    		gets(sVal);
    		pRec->sLoc = sVal;
    		if((iVal = get_loc_id(sVal)) == 0)
    		{
    			printf("Department ID not found!");
    			do{
    				printf("\nPlease enter Department ID: ");
    				gets(sVal);
    				iVal = atoi(sVal);
    				printf("Debug iVal = %d",iVal);
    				getch();
    			}while((!chk_dept(iVal)) || iVal < 1)
    		}
    		pRec->iLoc = iVal;
    		pRec->iYear = // The current year
    		pRec->iMonth = // The current month
    		printf("Please enter temperature: ");
    		gets(sVal);
    		iVal = atoi(sVal);
    		printf("Debug iVal = %d",iVal);
    		getch();
    		printf("\n\nYou are about to add the following details:\n"
    			"\nDepartment ID:   %d"
    			"\nDepartment Name: %s"
    			"\nTemperature:     %d"
    			"\n\nTo be added for %s %d."
    			"\n\nIs this information correct? ",
    			pRec->iLoc,pRec->sLoc,pRec->iTemp,get_month(pRec->iMonth), pRec->iYear);
    		gets(sVal);
      }while(!UCase(sVal[0]) == 'Y');
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There are functions in the time.h header.
    Code:
    time_t time;
    tm *pTime;
    
    time(&time);
    pTime = gmtime(&time);
    pTime-> ...
    tm is a struct that holds all values you want as fields.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    jim mcnamara
    Guest
    Code:
    #include <time.h>
    #include <stdio.h>
    int main(void){
          struct tm *t;
          time_t lt;
          int year, month;
          lt = time(NULL);
          t = localtime(&lt);
          year = t->tm_year + 1900;
          month = t->tm_mon + 1;
          printf("year = %d month = %d \n",year,month);
          return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width