Good evening. Today, I was writing a piece of code in Python. I have a string that is called date. It holds the following data:
date='05/04/2014'
So. I want to split this string into several substrings, each holding the day, month or year. These substrings will be called day, month and year, with the respective number in each string. How could I do this?
Also, I would like this method to work for any other date strings, such as:
02/07/2012
Simply use:
day,month,year = date.split('/')
Here you .split(..) the string on the slash (/) and you use sequence unpacking to store the first, second and third group in day, month and year respectively. Here date is the string that contains the date ('05/04/2014') and '/' is the split pattern.
>>> day,month,year = date.split('/')
>>> day
'05'
>>> month
'04'
>>> year
'2014'
Note that day, month and year are still strings (not integers).
Related
I have a date column that is of a string format. The dates appear like,
DDMMYYY or DDMYYYY
04032021 0412021
It has been a nightmare trying to split them and add '-' because they aren't all the same length. Not like a standard format of DDMMYYYY. I have also tried to split them by position. I have tried to cast it(the column) as a date.
Do you all have any suggestions for a way to get the entire column properly formatted and displaying as a date column? I need to filter on it.
It seems that the month and the year are on a fixed position relative to the start resp. end of the string.
So a trivial solution using substring would extract them and interpret the rest (padded with zeroes to the length of two) as the month.
select
dt,
substring(dt,1,2) dd,
LPAD(substring(dt,3,length(dt)-6),2,'0') mm,
substring(dt,length(dt)-3) yyyy
from T;
dt |dd|mm|yyyy|
--------+--+--+----+
04032021|04|03|2021|
0412021 |04|01|2021|
Conversion to date data type is a simple concatenation and a call of to_date
select
dt,
to_date(
substring(dt,1,2)|| -- dd
LPAD(substring(dt,3,length(dt)-6),2,'0')|| -- mm
substring(dt,length(dt)-3) -- yyyy
,'DDMMYYYY' ) ddt
from T;
dt |ddt |
--------+----------+
04032021|2021-03-04|
0412021 |2021-01-04|
I am doing a program in pyhon(3.10.1) in which i need to compare an input, in this case the number of a month (for example 05 for may) with the local current time.
from datetime import datetime
month=int(input("insert the number of a month: "))
dt_obj = datetime.now()
dt_frt = dt_obj.strftime("%m")
print(dt_frt)
if (month==dt_frt):
print("the month you selected is equal to the current month")
Then it stops at printing the current month, but does not do what the two last lines say, and it does not give any error. can anybody help me? Thanks.
I think you're comparing int with str, that's why the if condition isn't true. To fix this, you can remove the int(input(...)) or add a str(...) around your dt_frt
You can also get the month with just date.month (doc)
I have a function that checks if a date ( int number ) that is written in this format: "YYYYMMDD" is valid or not.
My question is how do i get to the first 4 numbers for example ( the year )?
the month ( the 5th and 6th number ) and the days.
Thanks
Probably the easiest way would be to convert it to a string and use substrings or regular expressions. If you need performance, use a combination of modulo and division by powers of 10 to extract the desired parts.
There is a simple way converting to string then slicing it:
Example for year:
date = str(date)
year = date[0:3]
for your example using the following format "YYYYMMDD"
I have understand that there are some functions days() in sybase iq, but in sybase ase, I think we have only date part option to get the number of days between two days.
My requirement is that i want number days between two days without comparing month or year between two days.
Any help appreciated!
Use datediff function:
Description
Calculates the number of date parts between two specified dates or times.
Syntax
datediff(datepart, {date, date | time, time | bigtime, bigtime | datetime, datetime | bigdatetime, bigdatetime}])
Parameters
datepart
is a date part or abbreviation. For a list of the date parts and abbreviations recognized by Adaptive Server, see Transact-SQL Users Guide.
date expression1
is an expression of type datetime, smalldatetime, bigdatetime, bigtime, date, time, or a character string in a datetime format.
date expression2
is an expression of type datetime, smalldatetime, bigdatetime, bigtime, date, time, or a character string in a datetime format.
Example 6
Finds the number of days between two times:
declare #a time
declare #b time
select #a = "20:43:22"
select #b = "10:43:22"
select datediff(dd, #a, #b)
-----------
0
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});