I have a character variable with a the value like this:
Aug 1, 2015
I want to have this date value in a date9. and a DDMMYYD10. format.
This is what I have tried:
month=upcase(substr(startdato,1,3));
day=0!!substr(startdato,5,1);
year=substr(startdato, 8,4);
startdato_a=trim(day)!!trim(month)!!trim(year);
FORMAT Startdato2 date9.;
format startdato3 DDMMYYD10.;
Startdato2 = INPUT(startdato_a,date9.);
Startdato3 = INPUT(startdato_a,date9.);
I get this output:
month=AUG
day=01
year=2015
startdato_a=01AUG2015
startdato2=.
startdato3=.
Why don't I get values in startdato2 and startdato3?
You are getting missing values due to leading / trailing whitespace in startdato_a, which prevents the informat from working properly. If you do input(strip(startdato_a),date9.) instead, it works as expected.
However, there is a much simpler way of doing this:
data want;
textdate = 'Aug 1, 2015';
date = input(textdate,anydtdte11.);
format date date9.;
run;
Output your character variables using the $QUOTE. function is a handy way to see leading blanks. Doing this you can see the what is causing the trouble with the INPUT() function.
startdato="Aug 1, 2015"
month="AUG"
day=" 01"
year="2015"
startdato_a=" 01AUG2015"
The cause of this is including a numeric constant when generating that character string.
day=0!!substr(startdato,5,1);
SAS had to convert the 0 into a string so it used best12. format which is why there are 11 leading blanks in the value of DAY. You could have use a string literal instead.
day='0'!!substr(startdato,5,1);
Which would yield better results.
startdato="Aug 1, 2015"
month="AUG"
day="01"
year="2015"
startdato_a="01AUG2015"
Startdato2=01AUG2015
startdato3=01-08-2015
Related
From reading code from elsewhere, I have a matrix of dates called 'time' that have unwanted spaces that I want removed.
I've tried isspace and regexprep with no luck
time = regexprep(time, '\W', '');
I have about 130000 dates with the following format:
04-July -2017 09:54:30.000
04-July -2017 09:54:31.000
etc
There are two spaces between the end of 'July' to the next dash I want to suppress to:
04-July-2017 09:54:30.000
04-July-2017 09:54:31.000
Replace two or more spaces with nothing:
>> time = {'04-July -2017 09:54:30.000'
'04-July -2017 09:54:31.000'}
>> regexprep(time,' {2,}','')
{'04-July-2017 09:54:30.000'}
{'04-July-2017 09:54:31.000'}
Unless you just want to correct your input file for later usage, you do not necessarily need to correct the input. There are several ways to parse the time directly with the extra spaces:
Let time be:
time = ['04-July -2017 09:54:31.000';
'04-July -2017 09:54:32.000']
Then to directly parse the string representation of the datetime into a MATLAB date serial number you can use:
%% get date in [MATLAB date serial number]
formatIn = 'dd-mmm -yyyy HH:MM:SS.FFF' ;
matlabTime = datenum(time,formatIn)
matlabTime =
736880.412858796
736880.41287037
This serial time representation is not so human readable but it's the fastest thing you can get if you want to do calculations with date/time.
If your goal is simply to correct the string, then you can use the same trick to read the value in, and define exactly which output format you want out:
%% get date in [string]
formatIn = 'dd-mmm -yyyy HH:MM:SS.FFF' ;
formatOut = 'dd-mmm-yyyy HH:MM:SS.FFF' ;
stringTime = datestr(datenum(time,formatIn),formatOut)
stringTime =
04-Jul-2017 09:54:31.000
04-Jul-2017 09:54:32.000
If you want to use the new datetime objects, the input format has a slight different syntax but the operation is roughly the same:
%% get date in [datetime] objects
formatIn = 'dd-MMM -yyyy HH:mm:ss.SSS' ;
t = datetime(time,'InputFormat',formatIn)
t =
04-Jul-2017 09:54:31
04-Jul-2017 09:54:32
Although the MATLAB console display t in human readable format, t is now a datetime object. Check the documentation if you want to use this.
Replace only two white-spaces after a month and preceding a dash (-):
>> date = '04-July -2017 09:54:30.000';
>> regexprep(date, '(\w) -', '$1-')
ans =
'04-July-2017 09:54:30.000'
I have a numeric vector corresponding to dates in the following format yyyymmdd, ie for December 24th, 2010 it is 20101224. How can I convert it into text format, i.e. in the following format 'mm-dd-yyyy'?
You should really use datetime rather than convert to strings,
dates = datetime(20100124,'ConvertFrom','yyyymmdd')
The first input can be a numeric vector, assuming it's of the yyyymmdd format.
If you then want to specify a display format use,
dates.Format = 'MM-dd-yyyy'
If you really need them as strings you can then use,
dates = datestr(dates)
Matlab has a datestr command which might be useful. Example usage:
formatOut = 'mm-dd-yyyy';
datestr(now,formatOut)
For your date, you could convert the input number to a string, convert the string to a date and create a date string with the new format.
formatIn = 'yyyymmdd';
formatOut = 'mm-dd-yyyy';
inStr = num2str(20101224); % Skip this step if already a number
outStr = datestr(datenum(inStr, formatIn), formatOut)
Very simple question. I'm using Matlab's datetime type, so I can carry timezone information. I need to get a specific string representation, to input into a DB. But datestr() does not have any fields to output tz info.
a = datetime('now', 'TimeZone', 'UTC');
%need output in the format 'YYYYMMDDTHH:MM:SS+00:00'
Any thoughts?
You can get the output you want by setting the Format property of the datetime object to display the time zone offset, converting it to a character array, then replacing the space by 'T':
>> a = datetime('now', 'TimeZone', 'UTC', 'Format', 'yyyyMMdd HH:mm:SSxxxxx')
a =
datetime
20171002 21:37:74+00:00
>> out = strrep(char(a), ' ', 'T')
out =
20171002T21:37:74+00:00
Also, take note of the case of the letters in the format string, as that matters for some of them.
I also tried the following:
datestr('19-01-2004','dd-mm-yyyy')
ans =
26-06-0024
I am new to MATLAB, so I am not sure what else to check.
In the function datestr(), the 2nd parameter denotes how the output should look like. It doesn't say anything about the input.
Essentially, you try to perform 2 steps: parse a string and then format the parsed date again.
So you can do
n = datenum('19-01-2004','dd-mm-yyyy')
datestr(n, 'yyyy-mm-dd')
and you'll get an n of 731965 and a final output of 2004-01-19.
You can as well do
v = datevec('19-01-2004','dd-mm-yyyy')
datestr(v, 'yyyy-mm-dd')
and your v becomes [2004 1 19 0 0 0].
So remember: step 1 - parsing of input with the appropriate format string, step 2 - formatting of output with the wanted format string.
If you want to give the date in a "clean" and readable format, you could just do
v = [2004 1 19 0 0 0]
datestr(v, 'yyyy-mm-dd')
datestr(v, 'dd.mm.yyyy')
datestr(v, 'mm/dd/yyyy')
When using datestr to convert a date string from one form to another, the format of the input date string is limit to those listed here. The format of your input '19-01-2004' is 'dd-mm-yyyy' and is not one of the supported formats.
If we change the input string to '01/19/2004', which is the supported format 'mm/dd/yyyy', we get the correct output:
>> datestr('01/19/2004','dd-mm-yyyy')
ans =
19-01-2004
To circumvent the limited number of supported input formats, the documentation recommends using datenum first. So you can map your original input onto itself like:
>> datestr(datenum('19-01-2004','dd-mm-yyyy'),'dd-mm-yyyy')
ans =
19-01-2004
As for why MATLAB returns the date it does has to do with how it handles the unknown format.
I suspect whatever method they use to finally decide upon a format results in a really small date number, hence the year 24 output.
I need to print the date in the format of mm/dd/yyyy.
if the date is 4/24/2009 it should print the date as 04/24/2009.
that is zero padding is also needed..
I used date function to get the current date...but the date is getting in the format of m/dd/yyyy...
Tested in the immediate window and is working for me (output as a comment)
Format(Now, "MM/dd/yyyy") '04/29/2009
Format(Date, "MM/dd/yyyy") '04/29/2009
Format(CStr(Now), "MM/dd/yyyy") '04/29/2009
Format(Date$, "MM/dd/yyyy") '04/29/2009
Format(CDate(Date), "MM/dd/yyyy")'04/29/2009
So whether it is string or datetime should not matter.
Edit: Saw your comment to Fredrik. It doesn't matter how it looks like when you save it to the db table (column date format would be a property of the db and not your program's (or vb's) responsibility). Just format the value as and when you retrieve it from the db.
Note that the "/" character in date formatting functions has a special meaning, as "date separator". This means that i may be replaced with the date separator for the current locale that the program is executed in (here in Sweden it would be replaced with "-" for instance). In order to ensure that you do indeed get the "/" character in the output, I think this would work (I don't have a VB installation to verify with):
Format(date, "MM'/'dd'/'yyyy")
just for the record, escaping the slash will work
Format(dt,"MM\/dd\/yyyy")
Try the next code:
Format(dt,"MM/dd/yyyy")
When you enter date in whatever format it will convert default value so do one thing that in access change your data type date/time to text then it can't affect to your work sure.
I also use VB6 and need to format date in my txt report
this works for me
Format$(Now, "yyyy-mm-dd-00.00.00")
but only if I declare date as string
You can try like this also depending upon your requirement
Dim strDate As String
Dim strDate1() As String
strDate = FormatDateTime(Now, vbGeneralDate)
If InStr(strDate, " ") > 0 Then
strDate1 = Split(strDate, " ")
Dim datDate1 As Date
If Month(strDate1(0)) < 10 Then
txtDate.Text = "0" + strDate1(0)
Else
txtDate.Text = strDate1(0)
End If
Else
End If
Formatting DateTime as a string is straightforward. Often we use format patterns like "HH." But methods like ToShortDateString are also useful.
Example. First we get the current time through DateTime.Now. When you execute these code examples, the current DateTime will be different on your computer.
Here: A format string beginning with MMM (for the month) is used. Look at how the pattern matches up to the output of the program.
VB.NET program that uses format string with DateTime
Module Module1
Sub Main()
' Use current time.
' ... Use a format.
' ... Write to console.
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))
End Sub
End Module
Output
Feb Tue 21 13:26 2017
strDate = Format(strDate, "yyyy-mm-dd")
BillTime = Format(BillTime, "hh:mm:ss")