Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6726513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:56:11+00:00 2026-05-26T09:56:11+00:00

I am trying to use strptime(buf, &pattern,&result) to convert char[] containing date into tm

  • 0

I am trying to use strptime(buf, &pattern,&result) to convert char[] containing date into tm structure.

I am using function like this:

if(strptime(buf, &pattern,&result) == NULL)
   {
      printf("\nstrptime failed\n");
...

and everything works if my variables are defined like this:

char buf[] = "26/10/2011";
char pattern[] = "%d/%m/%y";
struct tm result;

but if I change them into:

char buf[] = "2011/26/10";
char pattern[] = "%y/%d/%m";
struct tm result;

I get “strptime failed”. Notice, that I have only put year in the beginning (both in buf and pattern).

Help appreciated. My final target is to convert string in this format: 2011-10-26T08:39:21

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:56:11+00:00Added an answer on May 26, 2026 at 9:56 am

    It’s because the lower case %y is for the two-digit year within the century. Try changing it to uppercase %Y and it will work okay. You can see this from the following program:

    #include <stdio.h>
    #include <time.h>
    int main (void) {
        char buf[] = "26/10/2011";
        char pattern[] = "%d/%m/%y";
        struct tm result;
        if (strptime (buf, pattern, &result) == NULL) {
            printf("strptime failed\n");
            return -1;
        }
        printf ("%d\n", 1900 + result.tm_year);
        return 0;
    }
    

    This outputs 2020, meaning that the year is being read as just the 20 portion of 2011, with the remainder being ignored. If you use upper-case %Y, it outputs the correct 2011 instead.

    Code that generates the conversion error using the reversed format:

    #include <stdio.h>
    #include <time.h>
    int main (void) {
        char buf[] = "2011/10/26";
        char pattern[] = "%y/%m/%d";
        struct tm result;
        if (strptime (buf, pattern, &result) == NULL) {
            printf("strptime failed\n");
            return -1;
        }
        printf ("%d\n", 1900 + result.tm_year);
        return 0;
    }
    

    will work fine (ie, output 2011) when you change the pattern value to "%Y/%m/%d".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to use regex to replace any white space with &nbsp;, inside of example
I have been trying use the edit_post_link() function to contain an image. All of
What I am trying to do is use the Detours library to hook into
I am trying use MySql and Entity Framework, using Connector/Net 6.1 with this as
I am trying use Date Picker and on Select i want to display the
I'm still very new to C and trying to learn how to use strptime
I'm trying use make a div's background transparent using a mixture of CSS3 rgba()
Im trying to convert a string such as 12 August 2011 into time_t or
I am trying use a javascript function while passing php variables in it. For
I am trying use a javascript function while passing php variables in it. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.