Convert date into numeric date - string

In Matlab, how can I convert a date into a numeric date?
For example, I want to convert '31-Jan-1990' to '19900131'.

You can use datestr to change the date format to 19900131, and then use str2double to convert it to a number:
numDate = str2double(datestr('31-Jan-1990','yyyymmdd'))
numDate =
19900131
If you want to keep the date as a string just remove str2double from the above code.

Here are two functions that are the most helpful and appropriate ones for this situation:
datenum and datestr
The first step is to convert your string to Matlab's date number, which can be later converted to any string format, or even do calculation for date or time. Here we use additional argument to help on conversion. You may also check here for format you like to construct.
daynum = datenum('31-Jan-1990','dd-mm-YYYY')
The second step is then straightforward. You use the date number to translate to the string with the format you want.
datestr(daynum,'YYYYmmdd');
You can sure combine both functions together
datestr(datenum('31-Jan-1990','dd-mm-YYYY'),'YYYYmmdd')
The result
>> datestr(datenum('31-Jan-1990','dd-mm-YYYY'),'YYYYmmdd')
ans =
'19900131'
Finally, use str2num to achieve what you want.

Related

Finding multiple dates from a string in Python without using parsing packages

Trying to find dates in a string, but code doesnt work when I use "/" as a date separator. Also if i enter multiple dates. it returns only one.
I'd like to use all valid date separators viz "/" "-" "." and get all the dates in the string.
Also i'd like to use all the date formats like ddmmyy mmddyy yymmdd yyyymmdd ddmmyyyy mmddyyyy.
str = " here is some text in 31-01-2019 my string 01/02/2019 for fun 02.02.2019"
match = re.search('\d{2}-\d{2}-\d{4}', str)
date = dt.strptime(match.group(), '%d-%m-%Y').date()
print(date)
re.findall(r'\b\d{2}[-\./]\d{2}[-\./]\d{2}\b|\b\d{4}[-\./]\d{2}[-\./]\d{2}\b|\b\d{2}[-\./]\d{2}[-\./]\d{4}\b',lsstr)
output : ['31-01-2019', '01/02/2019', '02.02.2019']
Use this regexp to find all the dates present in the string.

Oracle: Assistance in breaking apart a complex date string

I have a complex date string being read from a csv file. The format is unable to be processed by Oracle's TO_DATE function. Looking for an efficient method to break this string apart and return a date object, to insert into a DATE column. The suggested option of using TO_DATE with 'DD-MON-YY HH.MI.SS AM' does not work. Not variation of this will break up this particular string. Hence the need for a custom function. I have also tried with the 'HH.MI.SS.SSSSS AM' format which also does not work. I have found that if I drop the fractional seconds, it will work. If I run a regex to drop that portion, it should convert as expected.
The string is formatted as: 21-OCT-04 01.03.23.966000 PM
My initial thought is to break up by space first, resulting in three sub strings.
Then break the first substring by - and the second by ., and load the resulting pieces into a DATE object directly.
Is there a better method I could use?
Thank you, Allan
Use what you have, which is a timestamp literal, to create a timestamp, and then cast it "as date":
select
cast(to_timestamp('21-OCT-04 01.03.23.966000 PM', 'dd-MON-rr hh.mi.ss.ff AM') as date) dt
from dual;
DT
----------------------
2004/10/21 01:03:23 PM
(The output format depends on my specific session NLS_DATE_FORMAT, which I actually changed for this illustration to 'yyyy/mm/dd hh:mi:ss AM'.)

Changing a numeric to a string variable in Stata

I have a variable ShiftStart that is a numeric variable in the format 01jan2014 06:59:59 (and so on). I want to change this to a string variable so that I can then substring it and create variables based on just date and just time separately.
When I try
generate str20 string_shiftstart=string(ShiftStart)
I create a string but all of the cells have been converted to strange values ("1.70e+12" and so on).
How can I keep the original contents of ShiftStart when it is converted to a string?
It seems you have a variable formatted as datetime. If so, no need to convert to string. There are appropriate functions that allow you to manipulate the original variable. This is clearly explained in help datetime:
clear
set more off
*----- example data -----
set obs 5
gen double datet = _n * 100000000
format datet %tc
list
*----- what you want -----
gen double date = dofc(datet)
format %td date
gen double hour = hh(datet) + mm(datet)/60 + ss(datet)/3600
list
The reason you find your original result surprising is because you are not aware of the fact that underlying the datetime display format, is a numerical value.
A good read (aside from help datetime) is
Stata tip 113: Changing a variable's format: What it does and does not mean, The Stata Journal, by Nicholas J. Cox.
Edit
To answer your last question:
If you want to create an indicator variable marking pre/post periods, one way is using td() (see the help file). Following the example given above:
// before 04jan1960
gen pre = date < td(04jan1960)
Creating this indicator variable is not always necessary. Most commands allow the use of the if qualifier, and you can insert the condition directly. See help if.
If you mean something else, you should be more explicit.

Stata: how to change a string variable to a date?

I'm new to Stata, and I'm wondering how can I change a string variable which contains a date to a date format.
The data in the variable looks like this:
yyyy-mm-dd
Should I first remove the dashes so that Stata can recognize the format in order to later use gen var = date() ?
Thank you for your help.
The Stata date function is smart about removing separator characters. See help datetime_translation under the section "the date function"
If your dates are in v1 and in the form yyyy-mm-dd you can specify the commands:
generate v2 = date(v1, "YMD")
format %td v2
The YMD is called a mask, and it tells Stata the order in which the parts of the date are specified. The second line will assign the variable the Stata daily date format, which means that when you look at that variable in the data, it will be shown in human readable form. The date is stored, however, as the number of days since January 1, 1960.
The best way to experiment with the date function is to use the display command. The first line will display an integer representing the number of days since January 1, 1960. The second line will display the date in a human readable format.
display date("2013-08-14", "YMD")
display %td date("2013-08-14", "YMD")
you can look here to see how to convert to data in Stata or do like this
tostring datedx, replace
generate str4 dxyr1= substr(datedx,1,4)
generate str2 dxmo1 = substr(datedx,6,7)
generate str2 dxda1 = substr(datedx,9,10)
destring dx*, replace
gen datedx1 = mdy(dxmo1, dxda1, dxyr1)

MATLAB - Only First Letter of String is Printing

I am having an issue printing a string in MATLAB (2012a) using the fprtinf command (and sprintf).
I have an array of 12 dates (numeric). I am converting them to strings using the following command:
months = datestr(data(:,1)-365,12); %Mar13 format
I obtain the following (and desired) output when I call the months variable:
Jan12
Feb12
Mar12
Apr12
etc..
The issue is when I call the fprintf or sprintf, say with the following code:
fprintf('%s', months(1))
I will only get the first letter of the month and not the full string. Any idea how to make it print the full string?
Thanks!
The resulting data type for your months variable is an NxM character array. You need to process it as a cell array of strings instead.
dates = num2cell(data(:,1)-365)
months = cellfun(#(x) datestr(x,12),dates,'UniformOutput',false)
fprintf('%s', months{1})
should get you what you want.
Simply change your call to
fprintf('%s', months(1, :))
datestr returns the string of each of the supplied dates on a separate row.
Alternatively you could use the cellstr function to convert the result to a cell array (this would also work with non fixed-length date formats like 'dddd')
months = cellstr(months);
fprintf('%s', months{1});

Resources