Convert Text(Date in ISO 8601 format) to DateTime - Data Studio - datetime

I need to convert a text that is in ISO 8601 Date format, to DateTime in DataStudio. When the text comes with the hour and timezone parts having non-zero values, it works normally.
Code to convert: DATETIME_ADD(PARSE_DATETIME("%FT%R:%E3S",LEFT_TEXT(CreatedOn,23)), INTERVAL 11-CAST(SUBSTR(CreatedOn,24,3) as INT64) HOUR)
But when the hour and timezone parts come as zero, the conversion becomes null:
Code to convert: DATETIME_ADD(PARSE_DATETIME("%FT%R:%E3S",LEFT_TEXT(BGInvoiceDate,23)), INTERVAL 11-CAST(SUBSTR(BGInvoiceDate,24,3) as INT64) HOUR)

you can try:
PARSE_DATETIME("%FT%X",LEFT_TEXT(BGInvoiceDate,19))

Related

convert an epoch timestamp to datetime in azure data factory

I'm working with data flow in azure data factory and i tried to convert an epoch formatted timestamp to date.
the value of the timestamp is '1574067907751' and i tried expressions :
toDate(toTimestamp(1574067907751*1000l))
or
toDate(toTimestamp(toInteger('1574067907751')*1000l,'yyyy-MM-dd HH:mm:ss'))
there is any other way to do that ?
https://learn.microsoft.com/en-us/azure/data-factory/concepts-data-flow-expression-builder#convert-to-dates-or-timestamps
"To convert milliseconds from epoch to a date or timestamp, use toTimestamp(). If time is coming in seconds, multiply by 1,000.
toTimestamp(1574127407*1000l)
The trailing "l" at the end of the previous expression signifies conversion to a long type as inline syntax."

In R, take a date string and convert it to datetime format (using lubridate)

In R I am trying to take a date string and convert it to date time format using lubridate but anm getting an error that:
All formats failed to parse. No formats found.
Using this code:
lubridate::as_date("1/2/34")
Shouldn't this just return a formated date time?
as.Date or as_Date needs format. By default, it can parse if the format is %Y-%m-%d. Here, it is not the case. So
lubridate::as_date("1/2/34", format ="%d/%m/%y")
Or more compactly
lubridate::dmy("1/2/34")
Based on the string, it is not clear whether it is day/month/year or month/day/year. Also, for 2-digit year, there is an issue with prefix i.e. it can be either "19" or "20". Here, it would parse at "2034"

Convert date to other format other than YYYY-MM-DD

I need to convert input date coming in format YYYY-MM-DD. First I convert it into char by following:
TO_CHAR(<date_column>,'YY/MM/DD')
then try to convert it into date for this format:
to_date((TO_CHAR(<date_column>,'YY/MM/DD')),'YY/MM/DD')
As to_date always converts to default date type of YYYY-MM-DD. What other way can I use to convert this into other format. I am using Informatica Powercenter, so I can not find other function other than TO_DATE.
I think we are ignoring basics - to_date() converts to a date format. Now it can be displayed in dd/mm/yyyy depending on setup in your DB client or if you are dumping in a file probably YYYY-MM-DD. And on a date filed you cfan use TO_CHAR o convert it to any format.
So, if your input is a string and is in 'YY/MM/DD' then use TO_CHAR(TO_DATE(imp_yymmdd,'YY/MM/DD'),'DD/MM/YYYY') - output will be a string of your desired format i.e. DD/MM/YYYY.
If your input is a date then use TO_CHAR(imp_date,'DD/MM/YYYY') - output will be a string of your desired format.
Date datatype has no format. You can format a string representing a date.
Assuming your input is date, just use TO_CHAR(yourdate, 'desiredformat').
If your input is a string, you first need to convert it to date then back to string again, with the desired format applied. So:
TO_CHAR(TO_DATE(yourstring, "format-it-comes-in"), "desired-format")
Which in your case would be:
TO_CHAR(TO_DATE(yourstring, "YYYY-MM-DD"), "YY/MM/DD")

Convert local timestamp to RFC 3339 format in R

I pull the current datetime using Sys.time() and I get "2018-05-12 11:52:21 EDT". How do I convert this to an RFC 3339 formatted string taking into account the local time zone is not UTC?
Note: The RFC 3339 format of this date is: 2018-05-12T15:52:21Z
This is the simplest thing I could come up with:
format(lubridate::as_datetime(Sys.time()), "%Y-%m-%dT%H:%M:%SZ")
The pseudocode:
Use as_datetime() function from lubridate package, which by default, converts the timestamp into a POSIXct datetime representation using UTC as the timezone
Format that time using format()
Just wanted to add that if you want an offset with the colon you can use the following:
.dt <- format(lubridate::now(), "%Y-%m-%dT%H:%M:%S%z")
paste0(stringr::str_sub(.dt, 1, -3),":", stringr::str_sub(.dt, -2, nchar(.dt)))

What Type of date time is this?

I received these details from a function in Paypal. It shows 2012-05-07T07:00:00Z. However I do not know what type of date time is this. I need to know because I will be converting that to UNIX timestamp.
That timestamp is in ISO 8601 format. The T in the middle separates the date part (on the left, in yyyy-mm-dd format) from the time (on the right, in hh:mm:ss). The Z indicates that the timestamp is in UTC.
This is ISO 8601 format.
Example: 1336346440 (2012-05-06 23:20:40Z)

Resources