I am trying to use the day function in datetime as
day(dateserial, 'dayofyear')
instead the function day from finance package is called giving me day of month because there is a day function with a similar signature.
I couldn't find in the documentation how to distinguish same function names. I find recommendations on the internet saying I should remove package from path but that's absurd as these are standard MATLAB packages.
example:
day(now,'dayofyear')
Warning: Unless the first input argument is a date character vector, all subsequent arguments will be ignored.
In datevec (line 67)
In day (line 39)
ans =
23
It seems date and datetime are not compatible.
The function day in the finance toolbox take a serial date or character vector as an input. For example:
day(datenum(now));
The function day that you want takes a datetime array as an input.
day(datetime(2017,02,23,01,06,00), 'dayofyear');
ans =
54
Matlab uses the input type to determine which function to use.
Your input to day() needs to be a datetime (not a number). If it's a datetime then it will call the day() function/method that applies to datetimes.
dt = datetime( datevec(now) )
day(dt, 'dayofyear')
Related
Using Julia, I'd like the find the period between two points in time, t2 and t1, where both t1 and t2 are in the format "y-m-d". I'm able to get this value in days, but I'd like to covert it to years:
date_format = DateFormat("y-m-d")
age_days = Date(t2, date_format) - Date(t1, date_format)
age_years = Dates.Year(age_days)
The above consistently returns an error. Understand I could just do Dates.value(age_days)/365.25, but is there a way to do this utilizing a built in function?
This functionality can be found in DayCounts.jl:
julia> using Dates, DayCounts
julia> d1=Date("2020-02-14", dateformat"y-m-d");
julia> d2=Date("2021-02-15", dateformat"y-m-d");
julia> yearfrac(d1,d2,DayCounts.ActualActualISDA())
1.003069091997904
The tricky part is to decide what to use as numerator and denominator when calculating such values.
The above code used 2006 ISDA definitions, ยง4.16 (b) approach:
(# of days in standard year)/365 + (# of days in leap year)/366
There are many other implementations and they all seem to be implemented in that library.
Note that this is an additional answer to my previous post: Julia/Dates : convert a timespan from "days" to "year" or "Float64" that may be a better match here.
Data
I am trying to create a time dimension using this:
t1 = datetime(1901,1,1);
t2 = datetime(2016,12,31);
t = t1:t2;
And create a netCDF file using this
nccreate('prec.nc','Prec',...
'Dimensions',{'time' 42369 'lon' 135 'lat' 129},...
'Format', 'netcdf4');
What I have tried
ncwrite('prec.nc', 'time', t);
Error Message
Error using cast
Unsupported data type for conversion: 'datetime'.
Error in internal.matlab.imagesci.nc/write (line 778)
scale_factor = cast(1, class(varData));
Error in ncwrite (line 87)
ncObj.write(varName, varData, start, stride);
Question
How can I create a daily time dimension that I can write out to a netCDF file? What is the proper date type for this conversion?
NetCDF doesn't define a single native way of storing date/time values, but there are established conventions, as desribed here.
There are two strategies for storing a date/time into a netCDF variable. One is to encode it as a numeric value and a unit that includes the reference time, e.g. "seconds since 2001-1-1 0:0:0" or "days since 2001-1-1 0:0:0" . The other is to store it as a String using a standard encoding and Calendar. The former is more compact if you have more than one date, and makes it easier to compute intervals between two dates.
So you could:
a) Use datestr to convert it to a string value. The conventional date string format for data interchange is ISO 8601, which you can get in Matlab with datestr(myDateTime, 'yyyy-mm-ddTHH:MM:SS').
b) Convert it to a numeric value representing seconds or days since a reference "epoch" time. I'd suggest using the Unix epoch, since Matlab provides a convenient conversion function for this already: posixtime(myDateTime). Then specify your units for that variable in the NetCDF file as 'seconds since 1970-01-01 00:00:00'.
You probably want to make sure your datetimes are in UTC before encoding them in the NetCDF, so you don't have to worry about time zone issues.
I am trying to get a date from its integer components: I have day, month and year as variables (that can change, I don't want to hard code them), and I want to reunite them in a date variable.
For example, something like that;
myDay: 15
myMonth: 4
myYear: 2016
`date$(myYear,myMonth,myDay) --> should return 2016.4.15 (formatted as a date).
Any way to do that?
Thank you
q)d:3
q)m:8
q)y:2016
q)"D"$"." sv string (y;m;d)
2016.08.03
See cast vs tok - need to use different arguments depending on if what you're casting from is a string or not
I want to create a categorical variable based upon date and input the following code.
data temppricedata;
set SASHELP.PRICEDATA;
date_group='';
IF (date>='MAR2002'd) THEN
date_group='new';
IF (date<'MAR2002'd) THEN
date_group='old';
run;
However I got error like
ERROR: Invalid date/time/datetime constant 'MAR2002'd.
ERROR 77-185: Invalid number conversion on 'MAR2002'd.
I am sure the format follows sas date format which is MONYY.
I do not know how to correct this.
As #Jeff mentioned, the correct way for specifying SAS date constants is DDMONYY or DDMONYYYY.
data temppricedata;
set SASHELP.PRICEDATA;
length date_group $3.;
IF date >= '01MAR2002'd THEN date_group='new';
ELSE IF date < '01MAR2002'd THEN date_group='old';
run;
I have requirement where i find the type of field and then i show the value of that field,
I am using SysDictField.baseType() method to get the type of the field and table.(field) will give me the value but if the field type is Time and when i try to fetch the type using baseType() method it gives me as an Integer so it gives me value like 72000, 62000 etc.. instead of 6.00 PM, 7.00 AM etc..
I want to convert the integer value to Time type so i can display 6.00 PM, 7 AM. Please help me.
The AX TimeOfDay type is number of seconds from midnight.
You can use time2Str function or use div and mod operand to calculate hours and minutes.
Example:
info(time2Str(31501, TimeSeparator::Colon, TimeFormat::AMPM));