none
system date and time with "AM" and "PM" specified

    Question

  • Hi,

    i want to  display the system current time  with "am" and "pm".

    I can able to get time bt am and pm is missing.

    Now i am using the code to get time is

    ->

    TCHAR szTime[15]={0};

    SYSTEMTIME sysTime;
    GetLocalTime(&sysTime);

    _stprintf(szTime, _T("%02d:%02d "), sysTime.wHour, sysTime.wMinute);

    Can anyone helpme if u know..

    Thanks

    Monday, September 23, 2013 11:22 AM

Answers

  • i want to  display the system current time  with "am" and "pm".


    For alternatives, see:

    Time Management
    http://msdn.microsoft.com/en-us/library/w4ddyt9h.aspx

    strftime, wcsftime, _strftime_l, _wcsftime_l
    http://msdn.microsoft.com/en-us/library/fe06s4ak.aspx

    - Wayne

    Monday, September 23, 2013 11:28 PM
  • lets see, back in the good old days we had time.h to work with.......

    #include <ctime>
    
    double benchmark(void (*f) (void)) {
    	clock_t start, finish; // benchmark the run
        start = clock();
    	f();
        finish = clock();
        return (double)(finish - start) / CLOCKS_PER_SEC;
    }


    now the fun part is that this is a high precision clock compared to that piece of garbage on your wall

    now the Win32 struct is:

    typedef struct _SYSTEMTIME {
      WORD wYear;
      WORD wMonth;
      WORD wDayOfWeek;
      WORD wDay;
      WORD wHour;
      WORD wMinute;
      WORD wSecond;
      WORD wMilliseconds;
    } SYSTEMTIME, *PSYSTEMTIME;

    so yanking the time from this object will require some code to select the chunks you want

    void main()
    {
        SYSTEMTIME st, lt;
        
        GetSystemTime(&st);
        GetLocalTime(&lt);
        
        printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute);
        printf(" The local time is: %02d:%02d\n", lt.wHour, lt.wMinute);
    }
    
    

    Now all we need to do is detect that st.hour >12 then sout << st.hour%12 etc


    Corsair Carbide 300R with window
    Corsair TX850V2 70A@12V
    Asus M5A99FX PRO R2.0 CFX/SLI
    AMD Phenom II 965 C3 Black Edition @ 4.0 GHz
    G.SKILL RipjawsX DDR3-2133 8 GB
    EVGA GTX 260 + BFG GTX 260 SLI 55nm 216 core (GT200 Tesla)
    Asus PA238QR IPS LED HDMI DP 1080p
    ST2000DM001 & Windows 8.1 x64 Professional
    LG WH14NS40 14X Blu-Ray Writer BDXL 128GB
    Microsoft Wireless Desktop 2000
    Wacom Bamboo CHT470M

    Tuesday, September 24, 2013 12:08 AM

All replies

  • The variable wHour of SYSTEMTIME contains the hour. The value is between 0 and 23. If you want to have a AM / PM time you need to do it by yourself.

    Hours between 0..11 are AM, hours between 12 .. 23 are PM. You only need a if and add / subtract the value 12 to the wHours.

    Here an conversion example:

    http://www.mathsisfun.com/time.html


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Monday, September 23, 2013 11:31 AM
    Monday, September 23, 2013 11:30 AM
  • Hi,

    I have tried with this,,

    Bt the problem is if it is 12:30 am,the result may be wrong.

    sinece it is greater than 12.

    Error case:

    How to get am and pm between 12 am to 1 am

    and 12 pm to 1 pm.

    this is the code i am using

    TCHAR szTime[15]={0};
    SYSTEMTIME sysTime;
    GetLocalTime(&sysTime);
    int hour = sysTime.wHour;
    if (hour > 12) 
    hour -= 12;
    _stprintf(szTime, _T("%02d:%02d "), hour, sysTime.wMinute);

    CString time=szTime;
    if(sysTime.wHour>12)
    {
    time=time+"PM";
    }
    else
    {
    time=time+"AM";
    }

    this is not working for the above case:

    thanks..

    Monday, September 23, 2013 11:37 AM
  • You have "12 AM" if  wHour is 0.

    Youn eed to handle 12AM or the hour that is "0" in 24h system also. not only the stuff between 12:00 and 23:59 that repesents PM


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.

    Monday, September 23, 2013 11:51 AM
  • Hi,

    thanks for your reply..

    anyway in 24 hr format also ,

    for example 12:30 AM and 12:30 PM will come.

    how to differentiate these two times whether it is AM or PM...

    Thanks...for any reply...

    Monday, September 23, 2013 11:55 AM
  • anyway in 24 hr format also ,

    Also what? The 24h format begins with 0:00 and ends with 23:59, 24:00 is equal to 0:00 of the next day.

    for example 12:30 AM and 12:30 PM will come.

    What is the particular problem with this two times?

    Here the explanation of AM an PM once more? http://www.mathsisfun.com/time.html


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Monday, September 23, 2013 12:04 PM
    Monday, September 23, 2013 12:03 PM
  • If this is to difficuilt you can also use GetTimeFormat(Ex). I forgot to refer this function in one of my last posts.

    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Monday, September 23, 2013 12:48 PM
    Monday, September 23, 2013 12:46 PM
  • Hi,

    thank your ans..

    i am trying with this..

    How can i get the system time always in 24hr format independent of the system display time at the bottom(i.e calender time).

    Since if i change the format sometimes it is getting 12 hrs format..

    thanks...

    Monday, September 23, 2013 12:50 PM
  • Quick answer:  since you are using CString, you are probably using MFC.  Just use CTime::GetCurrentTime().Format().

    Long answer:  Calculating the correct display string yourself is complex.  You need to handle these 4 time periods entirely separately:

    12AM-1AM

    1AM-12PM

    12PM-1PM

    1PM-12 AM

    For example, your code does not correctly display "12:30 PM", it shows "0:30 AM" instead.

    Thanks,
    David

    Monday, September 23, 2013 2:34 PM
  • i want to  display the system current time  with "am" and "pm".


    For alternatives, see:

    Time Management
    http://msdn.microsoft.com/en-us/library/w4ddyt9h.aspx

    strftime, wcsftime, _strftime_l, _wcsftime_l
    http://msdn.microsoft.com/en-us/library/fe06s4ak.aspx

    - Wayne

    Monday, September 23, 2013 11:28 PM
  • Hi,

    According to your description, you can use strftime just as Wayne suggested.

    size_t strftime (char* ptr, size_t maxsize, const char* format,

                     const struct tm* timeptr );

    The format parameter can be like below. You can get what you like.

    C string containing any combination of regular characters and special format specifiers. These format specifiers are replaced by the function to the corresponding values to represent the time specified in timeptr. They all begin with a percentage (%) sign, and are:

    specifier

    Replaced   by

    Example

    %a

    Abbreviated weekday name *

    Thu

    %A

    Full weekday name *

    Thursday

    %b

    Abbreviated month name *

    Aug

    %B

    Full month name *

    August

    %c

    Date and time representation *

    Thu Aug 23 14:55:02 2001

    %C

    Year divided by 100 and truncated to integer (00-99)

    20

    %d

    Day of the month, zero-padded (01-31)

    23

    %D

    Short MM/DD/YY date, equivalent to %m/%d/%y

    08/23/01

    %e

    Day of the month, space-padded ( 1-31)

    23

    %F

    Short YYYY-MM-DD date, equivalent to %Y-%m-%d

    2001-08-23

    %g

    Week-based year, last two digits (00-99)

    01

    %G

    Week-based year

    2001

    %h

    Abbreviated month name * (same as %b)

    Aug

    %H

    Hour in 24h format (00-23)

    14

    %I

    Hour in 12h format (01-12)

    02

    %j

    Day of the year (001-366)

    235

    %m

    Month as a decimal number (01-12)

    08

    %M

    Minute (00-59)

    55

    %n

    New-line character ('\n')

    %p

    AM or PM designation

    PM

    %r

    12-hour clock time *

    02:55:02 pm

    %R

    24-hour HH:MM time, equivalent to %H:%M

    14:55

    %S

    Second (00-61)

    02

    %t

    Horizontal-tab character ('\t')

    %T

    ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S

    14:55:02

    %u

    ISO 8601 weekday as number with Monday as 1 (1-7)

    4

    %U

    Week number with the first Sunday as the first day of week one (00-53)

    33

    %V

    ISO 8601 week number (00-53)

    34

    %w

    Weekday as a decimal number with Sunday as 0 (0-6)

    4

    %W

    Week number with the first Monday as the first day of week one (00-53)

    34

    %x

    Date representation *

    08/23/01

    %X

    Time representation *

    14:55:02

    %y

    Year, last two digits (00-99)

    01

    %Y

    Year

    2001

    %z

    ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100)
      If timezone cannot be termined, no characters

    +100

    %Z

    Timezone name or abbreviation *
      If timezone cannot be termined, no characters

    CDT

    %%

    A % sign

    %

    This code sample for your reference.

    #include <stdio.h>      /* puts */
    
    #include <time.h>       /* time_t, struct tm, time, localtime, strftime */
    
    
    int main ()
    
    {
    
      time_t rawtime;
    
      struct tm * timeinfo;
    
      char buffer [80];
    
    
      time (&rawtime);
    
      timeinfo = localtime (&rawtime);
    
    
      strftime (buffer,80,"Now it's %H:%M%p.",timeinfo);
    
      puts (buffer);
    
      getchar();
    
      return 0;
    
    }

    Best Regards,

    May


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Tuesday, September 24, 2013 9:35 AM
  • Hi,

    According to your description, you can use strftime.



    May -

    I already provided the OP with a reference to strftime which contains everything that
    you posted. There was no need to repeat it.

    - Wayne

    Tuesday, September 24, 2013 4:29 PM
  • Hi Wayne and Vegan,

    I am sorry that I did not read carefully about the post, I just read OP's question and reproduce then post my solution. Encountered such a problem I feel very embarrassed, next time I will pay attention.

    Thanks for your suggestions.

    Best Wishes,

    May


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 25, 2013 1:32 AM