I am using Selenium and using xpaths and I am trying to convert a date. I do have the DateTime library.
I need the date to be 1/15/23 but every time it logs it to console as 01/15/23. I am not sure why it logs it as 01 and not 1 on the month.
This is what I am using:
result_format=%m/%d/%y 12:00 AM date_format=%m/%d/%Y
It wouldn't b a big deal but I am using that date to compare against another page and it fails because of the 01.
Thanks for any help!
result_format=%m/%d/%y 12:00 AM date_format=%m/%d/%Y
Expecting it to log like this: 1/15/23 12:00 AM
I have used the datetime library and converted datetime to str and removed 0 from prefix.
import datetime
time1 = datetime.datetime(2020, 5, 17).strftime("%m/%d/%y 12:00 AM").removeprefix("0")
print(f"time1: {time1}")
time2 = datetime.datetime(2020, 12, 17).strftime("%m/%d/%y 12:00 AM").removeprefix("0")
print(f"time2: {time2}")
Output
time1: 5/17/20 12:00 AM
time2: 12/17/20 12:00 AM
Hope this helps. Happy Coding :)
Related
I have a column which has values in string type like below:
31-Oct-2016 12:00 AM
31-May-2015 12:00 PM
I want to convert the above column values to timestamp in IMPALA. Tried with cast, to_timestamp and other ways , but it is either showing syntax error or Null as result.
Can you please suggest a solution
2nd Requirement
There is a column like below in string, I want it to be converted to timestamp alone.
31-Oct-2016 12:00
31-May-2015 12:00
please suggest a way, I'm new to Impala
Thanks in advance
You can use below code. Unfortunately, impala doesn't have am pm conversion capability, but you can use a little code to identify PM and add 12 hours to that to convert properly.
select
if (right('31-Oct-2016 02:09 PM',2)='PM',
to_timestamp('31-Oct-2016 02:09 PM','d-MMM-yyyy H:m') + interval 12 hours,
to_timestamp('31-Oct-2016 02:09 PM','d-MMM-yyyy H:m')
) ampm_timestamp
Second requirement -
Impala always expects 24hour hour format when it converts datetime. So, in your case for 12 AM scenario, we have to do some special logic like below.
First check if its 12 AM, then minus 12 hours, else check if its PM, then add 12 hours(which covers 12PM scenario) and finally if its any other AM, it simply converts to timestamp.
select
CASE WHEN right('31-Oct-2016 12:09 AM',2)='AM' AND RIGHT( SPLIT_PART('31-Oct-2016 12:09 AM',':',1),2)='12'
THEN to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm') - interval 12 HOURS
ELSE CASE WHEN right('31-Oct-2016 12:09 AM',2)='PM'
THEN to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm') + interval 12 HOURS
ELSE to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm')
END END AMPM_TIMESTAMP
I need to convert the following varchar column to a 24 hr database time format in Teradata.
1:00 AM
6:45 PM
10:15 AM
9:30 PM
9:45 AM
8:30 PM
1:15 PM
Any pointers to existing solutions will be very helpful. I have not been able to find it.
Add the leading zero if missing and then cast using a format:
cast(lpad(col,8,'0') as time(0) format 'HH:MIBT')
When i select date as sept 16,i get to see sept 16 6am and sept 17 data until 5:59 AM.
It is treating 24 hours from sept 16 am to sept 17 6am.
Is there any issue with date field or report filter issue?
BigQuery's date functions work based on UTC timezone.
The UI you are using probably converts them into your local timezone, and you are seeing the 6 hours difference.
For visibility purposes I am posting my comment as an answer:
As #Pentium10 mentioned:
BigQuery's date functions work based on UTC timezone. The UI you are using probably converts them into your local timezone, and you are seeing the 6 hours difference.
According to this Data Studio uses UTC standard time, but if your data set does not use UTC you can use the TODATE function to convert the date field to UTC.
Dim DespatchDate As DateTime
Dim ReceiptDate As DateTime
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
ReceiptDate = DespatchDate.AddDays(60) 'Returns #5/6/2017 12:00:00 AM#
I would expect "1/9/2017" the 1st September 2017
I also tried
ReceiptDate = DateAdd("d", 10, DespatchDate) which returned #3/17/2017 12:00:00 AM#
I must be doing something wrong but what?
The date literal in VB uses the US format regardless of where you are. Your code:
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
is creating the date March 7 2017. Read more on date literals or use the constructor on the date class that makes it more apparent what you are doing.
Update
In order to be sure what the date is and to avoid ambiguity, you can define it this way:
DespatchDate = New Date(2017, 7, 3) ' Without the time of day, 12AM is implied
DespatchDate = New Date(2017, 7, 3, 12, 34, 56) ' July 3 2017 12:34:56 PM
The good thing with this is you can see from intellisense while typing the code what each number means. And if someone else had written the code, you can invoke intellisense to see what each argument means. As you can see from what I highlighted in my screenshot, the time is in 24 hour format.
I am trying to remove the timestamp from my date values so I end up with something like mm/dd/yy or mm/dd/yyyy. I referenced many technet, stackoverflow, and w3schools articles but still can't get the date to appear correctly. All of my columns in the table are defined as a datetime and they come from the same table.
I am using convert statements like this: CONVERT(VARCHAR(10), E.PD_DT, 101) AS 'Paid Date'
In place of 101 I have used 10 and 11, still have the same issue with the data (below). What's happening is when the date value (not including the time) is 8 characters, I am getting an additional character from the time, as seen in the Claim Adjustment Date-10 and Claim Adjustment Date-11 columns. Here is my data:
Claim Paid Date-101 Claim Paid Date Claim Adjustment Date-10 Claim Adjustment Date-11 Claim Adjustment Date
10/23/2012 10/23/12 12:00 AM 9/4/2012 1 9/4/2012 1 9/4/12 12:00 AM
10/23/2012 10/23/12 12:00 AM 9/4/2012 1 9/4/2012 1 9/4/12 12:00 AM
10/23/2012 10/23/12 12:00 AM 9/4/2012 1 9/4/2012 1 9/4/12 12:00 AM
09/06/2011 09/06/11 12:00 AM 9/4/2012 1 9/4/2012 1 9/4/12 12:00 AM
10/23/2012 10/23/12 12:00 AM 8/21/2012 8/21/2012 8/21/12 12:00 AM
09/06/2011 09/06/11 12:00 AM 8/21/2012 8/21/2012 8/21/12 12:00 AM
The strange thing is that all of the dates in the "Claim Paid Date" column have a zero for padding if the month or day is < 10. This makes the convert come out just fine but where the month or day is < 10 and doens't have a zero is where I get my problem.
You have a string source, not a DATETIME source.
What happens with DATETIME:
SELECT GETDATE() -- DATETIME
,CAST(GETDATE() AS DATE) --DATE
,CONVERT(VARCHAR(10),GETDATE(),101) --101 format
Result:
DateTime Date 101 Format
----------------------- ---------- ----------
2013-09-24 13:58:48.880 2013-09-24 09/24/2013
What happens with strings parading as DATETIME:
DECLARE #fake_date VARCHAR(25) = '10/23/12 12:00 AM'
SELECT CAST(#fake_date AS DATETIME) --DATETIME
,CAST(#fake_date AS DATE) --DATE
,CONVERT(VARCHAR(10),#fake_date,101) --101 format
Result:
DateTime Date 101 Format
----------------------- ---------- ----------
2012-10-23 00:00:00.000 2012-10-23 10/23/12 1
So perhaps you want:
CONVERT(VARCHAR(10),CAST(#fake_date AS DATE),101)
The correct way to do this is to store the data as DATETIME in the first place. Currently you are storing the data as strings, and you shouldn't be doing this, for a variety of reasons:
you lose the ability to sort (9/1/2012 will sort after 12/12/2012) without converting first. Converting is expensive.
you lose the ability to perform meaningful range queries without converting first (again, 9/1/2012 > 12/12/2012)
you lose all means of built-in validation - anyone can enter 13/33/2999 or 02/32/2099 or simply foo as a "date"
you lose the ability to perform date-related functions such as DATEADD, DATEPART or DATEDIFF against these columns without first converting
you need to convert to another type first before you can perform a conversion that will allow you to do things like trim time or present the date in a specific format
If you can't fix the table design and want to continue storing dates using the wrong data type, then the next best thing is to simply format the string in the client. If the client knows it's a date/time, then using .Format() or .ToString() should allow you to present the date without the time in whatever format you want.
You should be presenting dates to users in unambiguous formats, since you never know when some of your readers will see 6/12/2012 and not be sure if that's June 12 or December 6 (Canadians and Americans will see those differently, for example). So you can use, as an example:
DateVariable.Format('yyyy-MM-dd')
This will present an unambiguous date like 2012-06-12, which can only be misinterpreted in some very isolated regions and age groups in France (where y-d-m seems to remain in vogue).
However, if you absolutely want to present the ambiguous format, you can just use:
DateVariable.Format('MM/dd/yyyy')
...with no reason to change your SQL query at all. If, for some reason, you absolutely want or need to do this at the query level, then you can use:
SELECT CONVERT(CHAR(10), CONVERT(DATE, col), 101) -- mm/dd/yyyy
If you want to use less ambiguous forms, like yyyymmdd or yyyy-mm-dd, you can use:
SELECT CONVERT(CHAR(8), CONVERT(DATE, col), 112) -- yyyymmdd
SELECT CONVERT(CHAR(10), CONVERT(DATE, col), 120) -- yyyy-mm-dd
But again, conversion at the database layer is expensive, whereas string formatting at the client layer (where you're already handling these things as strings at that final step) is relatively cheap. You should be keeping these as date values as long as possible, both on the way in and on the way out of the database. There is just no advantage to treating them as strings anywhere SQL Server is concerned.
So, summary:
fix the table
perform the conversion on the client (preferably to an unambiguous format)
if you can't perform the conversion on the client, use a double-nested CONVERT (again, preferably to an unambiguous format)