0

I have a variable called FOUNDATION_DATE which includes the following date observations in string format:

'01/Jan/12'
''
''
''
'01/Jan/08'
''
'01/Jan/44'
''
''
'14/Oct/08'
''
''
'12/Jul/04'
'03/Aug/05'
'20/Apr/10'
'30/Dec/98'
'09/Apr/16'
'01/Jan/10'
'01/Dec/01'
'01/Jan/93'

I am using the Matlab function datetime to transform the above observations in datetime data type. The code is

datetime(FOUNDATION_DATE,'InputFormat','dd/MMM/yy')

which provides the following results:

01-Jan-2012
NaT
NaT
NaT
01-Jan-2008
NaT
01-Jan-2044
NaT
NaT
14-Oct-2008
NaT
NaT
12-Jul-2004
03-Aug-2005
20-Apr-2010
30-Dec-1998
09-Apr-2016
01-Jan-2010
01-Dec-2001
01-Jan-1993

While for the majority of the cases the transformation is conducted properly, for the observation '01/Jan/44' this is not the case as the year becomes 2044. This issue appears in many other date observations of my variable (only a small sample is presented here) and it is quite strange that this issue appears for date observations for years before 1969.

Does anyone have a solution for accurately transforming these strings to datetime variables? Any explanation also why this happens?

1
  • 1
    Have a look at the datetime help. There is an option called PivotYear, which says: Start year of the 100-year date range in which a two-character year resides [...] The default is year(datetime('now')) - 50 which is 1969 as of today. Set PivotYear to year(datetime('now')) - 99, and you should be fine. Commented Mar 7, 2019 at 15:55

2 Answers 2

4

You want the 'PivotYear' option, which defines which 100-year date range the 2 digit date refers to:

datetime( '01/Jan/44', 'inputformat', 'dd/MMM/yy', 'pivotyear', 1930 )

So here the 100-year range is 1930 - 2029

The default as documented (therefore not very "strange"), is

year(datetime('now'))-50 % = 1969 at time of writing (2019)
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to enlarge this 100-year range?
@Whitebeard13 Logically it's impossible - how would you expect Matlab to handle 2019 (written as just "19") if your date range was 1900 to 2099?
1

When only 2 years are represented matlab makes an assumption on what the first two digits are, you can override this by:

startYear = year(datetime('now')) - 99;
datetime('01/Jan/69', 'InputFormat', 'dd/MMM/yy', 'PivotYear', startYear)

That will make any dates in 2 digits up until today be historic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.