parsedatestr

by Tim C. Lueth, SG-Lib Toolbox: SolidGeometry 5.6 - Text/Strs/Chars/Links
Introduced first in SolidGeometry 1.0, Creation date: 2013-01-08, Last change: 2025-09-14

parse a datestr and returns the corresponding datenum and datestr

Example Illustration

 missing image of parsedatestr(DStr,nf)

Syntax

[dn,ds]=parsedatestr(DStr,[nf])

Input Parameter

DStr: Datestr in unknown format
nf: format, default is 29

Output Parameter

dn: datenum
ds: datenum

Examples

[~,ds]=parsedatestr ('31.12.77')
[~,ds]=parsedatestr ('2011-december-31')
[~,ds]=parsedatestr ('2011-december-31',1)




Copyright 2013-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth

Algorithm (Workflow)

The function parsedatestr is designed to parse a date string (DStr) and return its corresponding datanum and datestr. The function is part of the SG-Library and was created by Tim Lueth in 2013.

Input Parameters

Output Results

Algorithm Steps

  1. Check if DStr is empty. If so, return empty dn and ds.
  2. Set nf to 29 by default. If a second argument is provided and not empty, use it as nf.
  3. Initialize basey to '2000'.
  4. Determine the separator character schar in DStr. First, check for '-', then for '.'.
  5. Extract the year Y using strtok with schar.
  6. Extract the day D by reversing DStr, using strtok, and reversing again.
  7. Extract the month M from the remaining part of DStr.
  8. Convert the month string M to a number using the helper function Nrofmon.
  9. Check if the day D is a four-digit year or longer than Y, or if schar is '.', and swap Y and D if true.
  10. If Y is less than four digits, prepend it with the appropriate number of digits from basey.
  11. Attempt to convert the date components to a date number dn using datenum and format it to a date string ds using datestr with nf.
  12. If an error occurs, set dn and ds to empty.

Helper Function: Nrofmon

The function Nrofmon converts a month string Mstr to its corresponding month number.

Switch Cases

Algorithm explaination created using ChatGPT on 2025-08-19 08:27. (Please note: No guarantee for the correctness of this explanation)

Last html export of this page out of FM database by TL: 2025-09-21