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);
Related
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'
I have a date and time which I assemble into a date + time from strings in the form
date_string = "2020-12-30" 'yyyy-mm-dd
date_code = CDate(date_string)
time_string = "00:00:00" 'hh:mm:ss
time_code = TimeValue(time_string)
date_time = date_code + time_code
Commonly the return looks like 05.01.2019 11:00:00, which is what I expect.
The returned values also all check out as TRUE if I test with IsDate(date_time)
Whenever the time is 00:00:00 however, I only get the date returned with no time appended. I dont quite understand this, since TimeValue(time_string)returns 00:00:00.
So it must be an issue when combining date and time to a date + time string.
Can someone please enlighten me why midnight somehow does no exist in Excel VBA or where my error in creating the time code is?
EDIT:
I try to explain my situation a bit better:
I do this date date/time stuff in code and then but the result in an array in a loop. Only later on it is written to a cell in a table.
By the time is is written into a cell, even custom formatting the cell to "DD.MM.YYYY hh:mm" does not show the time as it is completely missing from the cell value.
Do I neet to apply a format at the point of date_code + time_code?
Sometimes the answer can be so simple. Thanks to Variatus and Paul I checked formatting out.
I applied a date_time = Format(date_code + time_code, "dd.mm.yyyy hh:mm") in my code. Using this, my code runs as expected and 00:00:00 appears as expected, even in the cell values of the Excel table.
When you enter an integer, like 43930, in a cell Excel will record the number as an integer, just as you entered it. You can then proceed to format the cell as #,##0.000 and thereby make the number display as 43930.000. Or you can format that very same number as custom dd mmm yyy hh:mm:ss and display it as 09 Apr 2020 00:00:00. The point is that Excel chose to record the number in its most efficient way, as an integer.
So, if you enter a DateValue + TimeValue which, together, amount to an integer Excel will record the integer correctly. The format in which that integer is displayed in your worksheet is a matter for cell formatting.
I have a VBA script at the moment that reads in 6 values as integers. 3 values for date, month, and year and 3 of the same type from a different location. I'd like to take the values and check if there has been more than a year to pass between them. DateDiff seems like the easiest way to handle this, however that function reads in the values as one DateValue (ie March 20, 2015) and my values are returned individually (08,08,2015).
I wrote a function that stores each value into a var and then using those individually I concat them into a format that DateValue can use.
This works, however I am curious if there is another(better) way to handle this problem?
Thanks in advance.
The function DateSerial(2015,3,4) will return the date for 3/4/2015, and with both of your dates in that format, you can subtract the one date from the other, and if the (absolute value of the) difference is > 365 then you know that they are over a year apart.
Dim A As Date
A = DateSerial(2015, 3, 4)
Dim B As Date
B = DateSerial(2014, 3, 2)
Dim C As Integer
C = Abs(A - B)
MsgBox C & " days between"
Hello everyone! I'm writing a lot of scripts these days at work but now I'm stuck...
Basically I need to analyze a parameter (a String) and see if the date it contains is later than another one...
I know it could be a "nooby" question, but the fact that the parameter is a string and the control is on a date totally confuses me...
Is it something as simple as: if parameter("DataAct") > 01/01/2010 ?!?!
Have a look at DateDiff function: http://www.w3schools.com/vbscript/func_datediff.asp. Example:
diff = DateDiff("d", "02/19/2015", "02/20/2015") ' difference in days
diff = DateDiff("h", "02/19/2015", "02/20/2015") ' difference in hours
diff = DateDiff("n", "02/19/2015", "02/20/2015") ' difference in mins
diff = DateDiff("s", "02/19/2015", "02/20/2015") ' difference in seconds
To calculate the difference, you would need to parse out the date from the string and use DateDiff.
The order of dates determines the output. In the example above, all values will be positive. If you revert them, output would result in negative. Providing the same date/time will result in 0.
diff = DateDiff("d", "02/20/2015", "02/19/2015") ' output = -1
diff = DateDiff("d", "02/20/2015", "02/20/2015") ' output = 0
I am trying to find out the difference between the system date and the date stored in the worksheet. If the difference between them is > 30 days, the result is true, else the result is false
Dim result as boolean
Dim sDate as string
sDate = Date
if Worksheets("dates").Cells(1,1) - sDate > 30 then 'how do I do this?
result = true
else
result = false
end if
How do I find out the difference in days between the system date and the date stored in the worksheet? The date in the worksheet can be a past date, too.
I wonder why I rarely see people using the date functions.
You can also use this:
if DateDiff("d", date1, date2) > 30 then
in this case, date1 would be CDate(Worksheets("dates").Cells(1,1))
and date2 would be sdate (either cast with CDate or dim'd as a date as Jeff said.
"d" means we are getting the difference in days. Here are the intervals for years, months, etc. in VBA:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week
h - Hour
n - Minute
s - Second
Try this:
if CDate(Worksheets("dates").Cells(1,1)) - sDate > 30 then
sDate is a STRING, which is NOT a Real Date!
Convert your string to a date, using either the CDate() function or the DateValue() function.
However, there is a caveat in this kind of conversion. These conversion will handle the following structures:
yyyy/mm/dd
yyyy/m/d
mm/dd/yyyy
m/d/yyyy
These will not be correctly converted
dd/mm/yyyy
d/m/yyyy
And avoid using any 2-digit year.
I would advise using the DateSerial() function for date conversion.
So regarding your code, assuming that the values on yor sheet are actually dates (to be certain, simply select the column and change the Number Format to GENERAL. If they are real dates, each will display a PURE NUMBER. Remember to hit UNDO to get your Date Format back)
Dim result As Boolean
If Worksheets("dates").Cells(1, 1).Value - Date > 30 Then
result = True
Else
result = False
End If