date format dd.mm.yyyy in C


I want to know if there is a way to read date from console in format dd.mm.yyyy in C. I have a structure with information for the date. I tried with another structure just for the date with day, month and year in it:

typedef struct { int day; int month; int year; } Date;

but the dots are a problem. Any idea?



Date d; if (scanf("%d.%d.%d", &d.day, &d.month, &d.year) != 3) error();


You can use <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html" rel="nofollow">strptime()</a> to read in an arbitrary formatted date string into a struct tm.

#define _XOPEN_SOURCE /* glibc2 needs this to have strptime(). */ #include <stdio.h> #include <string.h> #include <time.h> #include <errno.h> ... Date d = {0}; char * fmt = "%d.%m.%Y"; char s[32] = ""; char fmt_scanf[32] = ""; int n = 0; sprintf(fmt_scanf, "%%%ds", sizeof(s) - 1); /* Created format string for scanf(). */ errno = 0; if (1 == (n = scanf(fmt_scanf, s))) { struct tm t = {0}; char * p = strptime(s, fmt, &t); if ((s + strlen(s)) != p) { fprintf(stderr, "invalid date: '%s'\n", s); } else { d.day = t.tm_mday; d.month = t.tm_mon + 1; /* tm_mon it zero-based. */ d.year = t.tm_year + 1900; /* tm_year is years since 1900. */ } } else { perror("scanf()"); }


The positive side effects and addtional gains of going this way are:

<ul><li>No input validation is necessary, as it's all done by strptime().</li> <li>Changing the input format is trivial: Just let fmt point to a different format string.</li> </ul>


  • Compile Python 2.5.5 on OS X 10.6
  • Implicit Declaration of Function in C UNIX
  • How to format date to 1900's?
  • How do I convert dates in this format to a date class in R?
  • Can I disable openpyxl from automatically parsing strings to datetime?
  • Testing facebook connect on localhost
  • Violates RFC 2109: host > minus domain may not contain any dots
  • Calculating Digital Root, is there a better way?
  • Making a switch statement in C with an array?
  • Problem glTexGen in Open GL ES 2.0
  • Use of qualified name in function parameter
  • How to delete a newline using \\b
  • PHP: Very simple Encode/Decode string
  • Calling C function from lua
  • allocating memory to an array of string
  • quiver not drawing arrows just lots of blue, matlab
  • Zoom in and out of jPanel
  • Debug.DrawLine not showing in the GameView
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Error when parsing timestamp with pandas read_csv
  • C++ Partial template specialization - design simplification
  • Repeat a vertical line on every page in Report Builder / SSRS
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • Java applet as stand-alone Windows application?
  • Join two tables and save into third-sql
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Converting MP3 duration time