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
Syntax
[dn,ds]=parsedatestr(DStr,[nf])
Input Parameter
DStr: | | Datestr in unknown format |
nf: | | format, default is 29 |
Output Parameter
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
- DStr: A date string in an unknown format.
- nf: An optional format parameter, defaulting to 29 if not provided.
Output Results
- dn: The date number representation of the input date string.
- ds: The date string representation of the input date string.
Algorithm Steps
- Check if
DStr is empty. If so, return empty dn and ds.
- Set
nf to 29 by default. If a second argument is provided and not empty, use it as nf.
- Initialize
basey to '2000'.
- Determine the separator character
schar in DStr. First, check for '-', then for '.'.
- Extract the year
Y using strtok with schar.
- Extract the day
D by reversing DStr, using strtok, and reversing again.
- Extract the month
M from the remaining part of DStr.
- Convert the month string
M to a number using the helper function Nrofmon.
- 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.
- If
Y is less than four digits, prepend it with the appropriate number of digits from basey.
- 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.
- 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
- 'jan': Returns 1.
- 'feb': Returns 2.
- 'mrz', 'mar', 'mär': Returns 3.
- 'apr': Returns 4.
- 'mai', 'may': Returns 5.
- 'jun': Returns 6.
- 'jul': Returns 7.
- 'aug': Returns 8.
- 'sep': Returns 9.
- 'oct', 'okt': Returns 10.
- 'nov': Returns 11.
- 'dec', 'dez': Returns 12.
- Otherwise: Attempts to convert
Mstr to a number.
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