I get a raw data file that has two fields: Start_Date and Start_Time. They are initially String variables, where Start_Date is in MM/DD/YYYY and Start_Time is in hh:mm:ss formats.
I'd like to combine these into a single Date variable (MM/DD/YYY hh:mm:ss). Here is the syntax I'm using but it's clumsy:
String MyDate(A20).
Compute MyDate = Concat(CHAR.SUBSTR
(Start_Date, 4, 2), '-', CHAR.SUBSTR (Start_Date, 1,2), '-',
CHAR.SUBSTR (Start_Date, 7,4), ' ', Start_Time).
Execute.
Alter Type MyDate (DATETIME20).
Execute.
Creating some example data:
data list list/Start_Date Start_Time (2a10).
begin data
"09/18/2018" "18:15:13"
end data.
Now use the following syntax to combine the two texts into one date-time variable:
compute StartDT=sum(number(Start_Date, adate10), number(Start_Time, time8)).
formats StartDT (datetime20).
Related
I have a G1 table (Gramex) in UI which has columns with date and time but as string.
When the user sorts the respective column with the help of inbuilt sort option, it sorts the data as string not as date.
Approach 1
The easiest way would be if the date and time were formatted like YYYY-MM-DD HH:MM:SS. For example, '2022-09-04 03:02:01'. This is a string that can be sorted consistently with the datetime.
You can create such strings in Python with the datetime.isoformat() function.
Approach 2
If your dates are stored in a different format, e.g. 04-09-2022, etc., then IF the data is loaded from a file, not a database, then you can modify the data with the function: transform to convert the column to the right data format.
For example:
url:
continent:
pattern: /data
handler: FormHandler
kwargs:
url: data.csv
function: data.assign(date_col=pd.to_datetime(data[date_col]).strftime('%Y-%m-%dT%H:%M:%S'))
This converts the date_col (replace this with your date column name) from any date format Pandas recognizes into a YYYY-MM-DD HH:MM:SS type format that is sortable.
If the data is loaded from a database, then you DO need to convert it into a sortable format first.
I want to get a flat array of dates in a date range for Items with many Bookings. A Booking has :start_date and :end_date.
example, Item has these bookings:
Booking A: 10/1/17 to 10/3/17
Booking B: 11/2/17 to 11/4/17
I tried using pluck. Item.bookings.pluck[:start_date, :end_date] and it gives me an array that looks like this [["10,01,17", "10,03,17"], ["11,02,17" , "11,04,17" ]]
What I need is a flat array with the date ranges like this ["10,01,17", "10/02/17", "10,03,17", "11,02,17" , "11,03,17", "11/04/17"]
Well, in order to get ranges, you need actual Date objects. To get those out of strings like yours, you'll need to use Date.strptime. Then you can build Ranges out of the date pairs, and then convert back to strings with Date#strftime:
require 'date'
format = '%m/%d/%y' # mm/dd/yy; adjust as needed
dates = Item.bookings.pluck(%i<start_date end_date>).map do |bounds|
Range.new(*bounds.map { |d| Date.strptime(d, format) }).map do |d|
d.strftime(format)
end
end.flatten
Which with your example data set gives me this:
["10/01/17", "10/02/17", "10/03/17", "11/02/17", "11/03/17", "11/04/17"]
I have a column in a DataFrame with dates in yyyymmdd format and I need to change it permanently to yyyy-mm-dd.
How can I do that?
Given the info you provided in your comment, the column values can't be in the form yyyy-mm-dd since the column dtype is int64.
You can change the column dtype to be str, but the data won't be useful (ie you won't be able to do any date-calculations on it, though <, > should still work but lexicographically). If that's still what you wish and assuming df is the dataframe and the date column name is date:
def format_date_col(x):
x = str(x)
return '-'.join([x[:4], x[4:6], x[6:]])
# or maybe like that for better readability:
x = str(x)
return '{year}-{month}-{day}'.format(year=x[:4], month=x[4:6], day=x[6:])
df['date'] = df['date'].apply(format_date_col)
A better approach would be to use actual date dtype:
from datetime import datetime
def format_date_col(x):
return datetime.strptime(str(x), '%Y%m%d')
df['date'] = df['date'].apply(format_date_col)
print df['date'].dtype
>> datetime64[ns]
Is it possible to format the output of NOW() to a string that displays YYYY-MM-DD?
This is the output of NOW(): 29/02/2012 12.07.37
The reason is, that I need to use the current date in a QUERY.
QUERY only accepts date in the format YYYY-MM-DD . I can't get a date directly from a cell, because it gets formatted as (even if I change the formatting): DD/MM/YYYY
Perhaps some regular expression?
If this is supposed to be an in-cell formula then you can use
=TEXT(NOW(),"yyyy-mm-dd")
I will follow JMax's suggestion and convert my comment to an answer.
Now() returns the current date and time as a number. The integer part gives the date and the fraction part gives the time. If you print or display that date, the default is to give the full date in what Microsoft think's is the local format.
Format(expn, fmt) allows you to convert an expression to a string. For example:
Dim DateStg as String
DateStg = Format(Now(),"yyyy-mm-dd")
fmt defines the format to which the expn is to be converted. fmt is a mixture of code letters (such as: "yyyy", "mm", "dd") and punctuation (such as "-"). "yyyy-mm-dd" appears to meet your current needs but you can also usethe following to format dates:
"mmm" to give three letter month (Jan, Feb, etc.)
"mmmm" to give full name of month (January, February, etc)
"ddd" to give three letter day of week (Mon, Tue, etc)
"dddd" to give full name of day of week (Monday, Tuesday, etc)
In VB.net you can do the following:
Dim dateStr As String = Now().ToString("yyyy-MM-dd")
In C# you can do it like this:
String dateStr = DateTime.Now.ToString("yyyy-MM-dd");
how to change the date format for example, the data in the database as input represent, 01-jan-10 but how do i get an output as 01-jan-2010. And there are some dates which are 01-dec-99, which are to be represented as 01-dec-1999.. How can this be done?
Thanks
You can date format with TO_CHAR like :TO_CHAR(<YOUR_DATE_COLUMN>, 'DD-MON-RRRR')
e.g:
SELECT TO_CHAR(SYSDATE-5000, 'DD-MON-RRRR') MYDATE FROM DUAL
UNION
SELECT TO_CHAR(SYSDATE, 'DD-MON-RRRR') MYDATE FROM DUAL