Solved

Date formats

Posted on 2001-06-21
7
608 Views
Last Modified: 2008-02-01
Hey,
I have the date format (retrived from GetLocaleInfo), and a spesipic date in that format.
I want to convert it to mm/dd/yyyy format...

How can I do it ?
(I'm working on NT/2K)

Thanks
  Gilad.
0
Comment
Question by:gshriki
7 Comments
 
LVL 11

Accepted Solution

by:
griessh earned 100 total points
ID: 6215948
I hope this will help you. It is from the VC++ help files:

strftime, wcsftime
Format a time string.

size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );

size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );

Routine Required Header Compatibility
strftime <time.h> ANSI, Win 95, Win NT
wcsftime <time.h> or <wchar.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

strftime returns the number of characters placed in strDest if the total number of resulting characters, including the terminating null, is not more than maxsize. wcsftime returns the corresponding number of wide characters. Otherwise, the functions return 0, and the contents of strDest is indeterminate.

Parameters

strDest

Output string

maxsize

Maximum length of string

format

Format-control string

timeptr

tm data structure

Remarks

The strftime and wcsftime functions format the tm time value in timeptr according to the supplied format argument and store the result in the buffer strDest. At most, maxsize characters are placed in the string. For a description of the fields in the timeptr structure, see asctime. wcsftime is the wide-character equivalent of strftime; its string-pointer argument points to a wide-character string. These functions behave identically otherwise.

Note   Prior to this version of Visual C++, the documentation described the format parameter of wcsftime as having the datatype const wchar_t *, but the actual implementation of the format datatype was const char *. In this version, the implementation of the format datatype has been updated to reflect the previous and current documentation, that is: const wchar_t *.

Generic-Text Routine Mappings

TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tcsftime strftime  strftime wcsftime


The format argument consists of one or more codes; as in printf, the formatting codes are preceded by a percent sign (%). Characters that do not begin with % are copied unchanged to strDest. The LC_TIME category of the current locale affects the output formatting of strftime.(For more information on LC_TIME, see setlocale.) The formatting codes for strftime are listed below:

%a

Abbreviated weekday name

%A

Full weekday name

%b

Abbreviated month name

%B

Full month name

%c

Date and time representation appropriate for locale

%d

Day of month as decimal number (01 ? 31)

%H

Hour in 24-hour format (00 ? 23)

%I

Hour in 12-hour format (01 ? 12)

%j

Day of year as decimal number (001 ? 366)

%m

Month as decimal number (01 ? 12)

%M

Minute as decimal number (00 ? 59)

%p

Current locale?s A.M./P.M. indicator for 12-hour clock

%S

Second as decimal number (00 ? 59)

%U

Week of year as decimal number, with Sunday as first day of week (00 ? 53)

%w

Weekday as decimal number (0 ? 6; Sunday is 0)

%W

Week of year as decimal number, with Monday as first day of week (00 ? 53)

%x

Date representation for current locale

%X

Time representation for current locale

%y

Year without century, as decimal number (00 ? 99)

%Y

Year with century, as decimal number

%z, %Z

Time-zone name or abbreviation; no characters if time zone is unknown

%%

Percent sign

As in the printf function, the # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows.

Format Code Meaning
%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% # flag is ignored.
%#c Long date and time representation, appropriate for current locale. For example: ?Tuesday, March 14, 1995, 12:41:29?.
%#x Long date representation, appropriate to current locale. For example: ?Tuesday, March 14, 1995?.
%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y Remove leading zeros (if any).


Example

/* TIMES.C illustrates various time and date functions including:
 *      time            _ftime          ctime       asctime
 *      localtime       gmtime          mktime      _tzset
 *      _strtime        _strdate        strftime
 *
 * Also the global variable:
 *      _tzname
 */

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

void main()
{
    char tmpbuf[128], ampm[] = "AM";
    time_t ltime;
    struct _timeb tstruct;
    struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

    /* Set time zone from TZ environment variable. If TZ is not set,
     * the operating system is queried to obtain the default value
     * for the variable.
     */
    _tzset();

    /* Display operating system-style date and time. */
    _strtime( tmpbuf );
    printf( "OS time:\t\t\t\t%s\n", tmpbuf );
    _strdate( tmpbuf );
    printf( "OS date:\t\t\t\t%s\n", tmpbuf );

    /* Get UNIX-style time and display as number and string. */
    time( &ltime );
    printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
    printf( "UNIX time and date:\t\t\t%s", ctime( &ltime ) );

    /* Display UTC. */
    gmt = gmtime( &ltime );
    printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );

    /* Convert to time structure and adjust for PM if necessary. */
    today = localtime( &ltime );
    if( today->tm_hour > 12 )
    {
   strcpy( ampm, "PM" );
   today->tm_hour -= 12;
    }
    if( today->tm_hour == 0 )  /* Adjust if midnight hour. */
   today->tm_hour = 12;

    /* Note how pointer addition is used to skip the first 11
     * characters and printf is used to trim off terminating
     * characters.
     */
    printf( "12-hour time:\t\t\t\t%.8s %s\n",
       asctime( today ) + 11, ampm );

    /* Print additional time information. */
    _ftime( &tstruct );
    printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
    printf( "Zone difference in seconds from UTC:\t%u\n",
             tstruct.timezone );
    printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
    printf( "Daylight savings:\t\t\t%s\n",
             tstruct.dstflag ? "YES" : "NO" );

    /* Make time for noon on Christmas, 1993. */
    if( mktime( &xmas ) != (time_t)-1 )
   printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );

    /* Use time structure to build a customized time string. */
    today = localtime( &ltime );

    /* Use strftime to build a customized time string. */
    strftime( tmpbuf, 128,
         "Today is %A, day %d of %B in the year %Y.\n", today );
    printf( tmpbuf );
}



Output

OS time:                                21:51:03
OS date:                                05/03/94
Time in seconds since UTC 1/1/70:       768027063
UNIX time and date:                     Tue May 03 21:51:03 1994
Coordinated universal time:             Wed May 04 04:51:03 1994
12-hour time:                           09:51:03 PM
Plus milliseconds:                      279
Zone difference in seconds from UTC:    480
Time zone name:                        
Daylight savings:                       YES
Christmas                               Sat Dec 25 12:00:00 1993

Today is Tuesday, day 03 of May in the year 1994.


0
 

Expert Comment

by:bvikas
ID: 6218998
If u can specify the format, u can use pointers in C. Store the date into an array(of length+1 of the format).point it to pointer and fill the date variables. Thereafter format them into another array.
Eg:

12122000 (12 dec 2000)
char date1[9],*p,dd[3],mm[3],yy[5],date2[11];
sprintf(date1,"12122000");
p=date1;
strncpy(dd,p,2);
dd[2]='\0';
p+=2;
strncpy(mm,p,2);
mm[2]='\0';
p+=2;
strncpy(yy,p,4);
yy[4]='\0';
sprintf(date2,"%s/%s/%s",mm,dd,yy);
variable date2 has the date in the format required by u.

Hope this code suffices u'r need.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6219246
Hi bvikas, welcome to EE

We usually try not to lock a question, but post an answer as a comment as suggested in the tips at the end of this page. This will give other experts a chance to bring new aspects in the discussion. The asker can still accept a comment as an answer. Your solution is made for a very specific case, that might not even come close to what Gilad is asking for.

======
Werner
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:Axter
ID: 6251335
Hi gshriki:
Feel free to click the [Reject Answer] button near (Answer-poster's)
response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other
experts.  Later, you can click the [Select Comment as Answer] button on any
response.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6402284
This question was LOCKED with a PROPOSED ANSWER and awaits your decision today.  Once a question is LOCKED with a Proposed Answer, few new experts will step in to help on that question, since the assumption is, you've been helped.  If the Proposed Answer helped you, please accept it and award that expert.  If it did not help you, please reject it and add comments as to status and what else is needed.
 
If you wish to award multiple experts, just comment here with detail, I'll respond as soon as possible.  As it stands today, you asked the question, got help and not one expert was awarded for the contribution(s) made.  Your response is needed.  I'll monitor through month end, and if you've not returned to complete this, we'll need to decide.  Expert input is welcome (as always) to determine the outcome here if the Asker does not respond.
 
Your response in finalizing this (and ALL) your question(s) is appreciated.
 
Moondancer
Community Support Moderator @ Experts Exchange

0
 
LVL 11

Expert Comment

by:griessh
ID: 6419740
Moondancer

It seems we are not getting any response here. I think I gave all the information the asker needed to come to the requested result (nobody else jumped in with more help ... ).

Maybe you should remind gshriki that he has several open question in EE.

Thanks

======
Werner
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6426671
Thank you, Werner, we're following with gshriki to finalize all open questions at Experts Exchange, and I've escalated the issue to Administration.

I am force accepting this to close and award this question.  In the event Asker returns with needs in this regard, please respond.

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now