I have a date store like a string in the format
anomesdia_mail='2014-01-31'
I would need to convert it to a date in order to compare it with another date:
sevendays_after='13FEB14:00:00:00'.
I tried this but didn't work:
anomesdia_mail1 = input(substr(strip(anomesdia_mail),1,10),MMDDYY10.);
and after this I would need to compare both:
sum(case when b.anomesdia_mail<=a.sevendays_after then 1 else 0 end)
Thanks!
Try treating sevendays_after as a datetime variable and use datepart to extract the date from it before you compare it with your date variable, e.g.
data _null_;
a = "2014-01-31";
b = "13FEB14:00:00:00";
a2 = input(a,yymmdd10.);
b2 = datepart(input(b,datetime.));
c= (a2 = b2);
put _all_;
run;
to convert it in a date I have to use the input function.
for example= mail date was a string in the format DD-MM-YYYY
so first:
I have to give it the proper order:
mesdiaano_mail_string = catx(separator,substr(mail_date,6,2),substr(mail_date,9,2),substr(mail_date,1,4));
then convert it in date format:
mail_date_int = input(substr(strip(mesdiaano_mail_string),1,10),MMDDYY10.);
with datepart function I can convert it to an integer with the numbers of days since 1960 and perform operation:
Related
how to separate date and time from datetime column if you have the format as below :
click here to view image
I am trying int(datetime column) for fetching date ; Datetime column - int(datetime column) for fetching time column
Your formula cannot work because your data is a text string (note that it has a letter included) and not a number.
So first convert the string into a "real" time with:
=substitute(a2,"T"," ")
You can then use:
Date: =INT(SUBSTITUTE(A2,"T"," "))
Time: =MOD(SUBSTITUTE(A2,"T"," "),1)
and be sure to format the results as desired:
If your column is formatted true date then use to separate date
=TEXT(A1,"yyyy-mm-dd")
For time
=TEXT(A1,"hh:mm:ss")
If data is in text string or output by TEXT() function then try below functions.
for date =TEXT(FILTERXML("<t><s>"&SUBSTITUTE(A1,"T","</s><s>")&"</s></t>","//s[1]"),"yyyy-mm-dd")
for time =TEXT(FILTERXML("<t><s>"&SUBSTITUTE(A1,"T","</s><s>")&"</s></t>","//s[last()]"),"hh:mm:ss")
For date
=LEFT(A2,FIND("T",A2)-1)
For time
=RIGHT(A2,LEN(A2)-FIND("T",A2))
I have problem in vba in excel.
I want the function to check whether the second day of the passed date has already been or not. I know that I'm comparison two string now, i dont know how change it to date format. Here is my code fragment:
Dim MyDate As Date
Set Passed = Range("D2")
MyDate = Format(DateTime.Now, "DD-DD.MM.YYYY")
If MyDate < Passed Then
Passed.Interior.ColorIndex = 45
ElseIf MyDate > Passed Then
Passed.Interior.ColorIndex = 43
The D2 is Short Date Format (eg. 02-05.02.2021)
You convert your String data type to a Date datatype using the CDate function.
For example CDate(Passed)
You will also need to remove the Format function as that converts your DatetTime.Now to a String
If CDate(Range("D2")) > DateTime.Now() Then
'Do Something
Else
'Do Something else
EndIf
i have a set of data for couple of days and the names of the data files like this
name='Newyork20200915'
which is for the 15th of September and i want to export only the date to excel like shown below
So how can i get the date from the name string ?
Thanks in advance
Assuming that the other part of name will not contain any digits besides the date, you can use regexp to get all the digits from the character array:
name = 'Newyork20200915'
date_only = regexp(name, '\d*', 'match')
Next, you can convert this date string to a serial date number using datenum, by providing the format in which the date is currently. And then use datestr to format it to your desired format.
date_formatted = datestr(datenum(date_only, 'yyyymmdd'), 'dd. mmm')
date_formatted =
'15. Sep'
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.
I have a series of dates and some corresponding values. The format of the data in Excel is "Custom" dd/mm/yyyy hh:mm.
When I try to convert this column into an array in Matlab, in order to use it as the x axis of a plot, I use:
a = datestr(xlsread('filename.xlsx',1,'A:A'), 'dd/mm/yyyy HH:MM');
But I get a Empty string: 0-by-16.
Therefore I am not able to convert it into a date array using the function datenum.
Where do I make a mistake? Edit: passing from hh:mm to HH:MM doesn't work neither. when I try only
a = xlsread('filename.xlsx',1,'A2')
I get: a = []
According to the documentation of datestr the syntax for minutes, months and hours is as follows:
HH -> Hour in two digits
MM -> Minute in two digits
mm -> Month in two digits
Therefore you have to change the syntax in the call for datestr. Because the serial date number format between Excel and Matlab differ, you have to add an offset of 693960 to the retrieved numbers from xlsread.
dateval = xlsread('test.xls',1,'A:A') + 693960;
datestring = datestr(dateval, 'dd/mm/yyyy HH:MM');
This will read the first column (A) of the first sheet (1) in the Excel-file. For better performance you can specify the range explicitly (for example 'A1:A20').
The code converts...
... to:
datestring =
22/06/2015 16:00
Edit: The following code should work for your provided Excel-file:
% read from file
tbl = readtable('data.xls','ReadVariableNames',false);
dateval = tbl.(1);
dateval = dateval + 693960;
datestring = datestr(dateval)
% plot with dateticks as x-axis
plot(dateval,tbl.(2))
datetick('x','mmm/yy')
%datetick('x','dd/mmm/yy') % this is maybe better than only the months
Minutes need to be called with a capital M to distinguish them from months.
Use a=datestr(xlsread('filename.xlsx',1,'A:A'),'dd/mm/yyyy HH:MM')
Edit: Corrected my original answer, where I had mixed up the cases needed.
I tried with this. It works but it is slow and I am not able to plot the dates at the end. Anyway:
table= readtable ('filename.xlsx');
dates = table(:,1);
dates = table2array (dates);
dates = datenum(dates);
dates = datestr (dates);