Any site around date format days that the conversion needs MMM but this wont work. When i do...
${date_to_search_for}= Convert Date 2017-06-14 13:03:02.506610 date_format=%Y-%M-%d %H:%m:%S.%f result_format=%d %MMM %Y 00:00:00 exclude_millis=True
I get
14 06MM 2017 00:00:00
Obviously am looking for Jun in this example
%MMM is not a valid directive in datetime. You want to use %b if you want a month's abbreviated name. Your result_format should be:
result_format=%d %b %Y 00:00:00
Related
Splunk convert Wed Sep 23 08:00:00 PDT 2020 to _time and epoch time in splunk . What is the splunk query to convert java date format to yyyy-MM-dd
From https://docs.splunk.com/Documentation/Splunk/8.0.6/SearchReference/DateandTimeFunctions#strptime.28X.2CY.29
| eval date_unix_timestamp=strptime(date, "%Y-%m-%d")
To convert time strings from one format to another you must strptime() convert to epoch form and then use strftime() to convert to the desired result format.
... | eval newTS=strftime(strptime("Wed Sep 23 08:00:00 PDT 2020", "%a %b %d %H:%M:%:S %Z %Y"), "%Y-%m-%d")
I am not sure what is going wrong here. I have given a time to convert, the format of the time, and the result format, like so:
${date_to_search_for}= Convert Date 2017-06-14 13:03:02.506610 date_format=%Y-%m-%d 00:00:00.00000 result_format=%d %b %Y 00:00:00 exclude_millis=True
Log to console ${date_to_search_for}
Running this code, presents this error:
ValueError: time data '2017-06-14 13:03:02.506610' does not match format '%Y-%m-%d 00:00:00.00000'
I have checked both formats closely and cannot see a difference! I don't understand why it is throwing this error.
The date you have given Robot:
2017-06-14 13:03:02.506610
Is not the format you are saying it is
date_format=%Y-%m-%d 00:00:00.00000
Please use the Python Formatting of DateTime like so:
${date_to_search_for}= Convert Date 2017-06-14 13:03:02.506610 date_format=%Y-%m-%d %H:%M:%S.%f result_format=%d %b %Y 00:00:00 exclude_millis=True
Log to console ${date_to_search_for}
Note the %H:%M:%S.%f for your time requirements.
%H = 24 Hour Hour Time
%M = Minute with leading 0
%S = seconds with leading 0
%f = microseconds with leading 0
All can be found Here
This will result in the following format to be logged:
${date_to_search_for} = 14 Jun 2017 00:00:00
Any problems please ask :)
I’m working with date strings in R. Essentially, I have three different strings that represent date variables. I have these weird date strings from scraping data on the web.
Is it possible to convert these three different date strings into a universal format that makes it easier to perform logic on them with basic R code? Here are what the strings look like. Any help is greatly appreciated.
1. "Wed, Feb 7, 2017 7:30 pm"
2. "Wed Feb 7 08:00:04 2017"
3. "2017-02-7 13:06:14 PST" # Sys.time()
UPDATE: I now have a better understanding of as.POSIXct now, but I still don't understand why this doesn't work ?
as.POSIXct('02/15/2017, 10:00 PM', format = "%M/%D/%Y, %H:%M %r")
While your specific question has already been answered in comments.
I would like to leave this as a general reference for other people who might have similar problems and can come across this question.
So, as you have this in d.b's comment, your data time string have been parsed via command:
as.POSIXct("Wed, Feb 7, 2017 7:30 pm", format = "%A, %b %d,%Y %H:%M")
The difference between your first and the second case was in the format.
So, this is a general guidance on the format:
%a Abbreviated weekday
%A Full weekday
%b Abbreviated month
%B Full month
%c Locale-specific date and time
%d Decimal date
%H Decimal hours (24 hour)
%I Decimal hours (12 hour)
%j Decimal day of the year
%m Decimal month
%M Decimal minute
%p Locale-specific AM/PM
%S Decimal second
%U Decimal week of the year (starting on Sunday)
%w Decimal Weekday (0=Sunday)
%W Decimal week of the year (starting on Monday)
%x Locale-specific Date
%X Locale-specific Time
%y 2-digit year
%Y 4-digit year
%z Offset from GMT
%Z Time zone (character)
This is also useful if you want to do the conversions between different formats:
x <- as.POSIXct( "2017-01-15")
format(x, "%a")
[1] "Sun"
format(x, "Week of the year: %W")
[1] "Week of the year: 02"
source: https://www.stat.berkeley.edu/~s133/dates.html
as.POSIXct("Wed, Feb 7, 2017 7:30 pm", format = "%A, %b %d,%Y %H:%M", tz="PST8PDT")
as.POSIXct("Wed Feb 7 08:00:04 2017", format = "%A %b %d %H:%M:%S %Y",tz="PST8PDT")
as.POSIXct("2017-02-7 13:06:14 PST", format = "%Y-%m-%d %H:%M:%S",tz="PST8PDT")
I need to format a date variable with R. I get the current date with a simple date().
today <- date()
today
> "Mon Oct 10 1:00 2016"
I need to format this 'today' variable into a string with a specific format. below is an example of what the string should look like.
string <- ”10/10/2016 1:00 PM EDT"
string
> ”10/10/2016 1:00 PM EDT"
So the question is how do you format a character string that looks like "Mon Oct 10 1:00 2016" into ”10/10/2016 1:00 PM EDT".
I've tried working with strptime() and as.Date() functions but cannot figure out how to convert this string into a formatted date. Thanks for any help.
strftime(strptime(date(),
format = "%a %b %d %H:%M:%S %Y"),
format = "%m/%d/%Y %I:%M %p %Z")
See ??strptime.
I am not getting the right conversion when I try to convert 12 hours to 24 hours. My script (with sample data is below).
library(lubridate)
library(data.table)
# Read Sample Data
df <- data.frame(c("April 22 2016 10:49:15 AM","April 22 2016 10:01:21 AM","April 22 2016 09:06:40 AM","April 21 2016 09:50:49 PM","April 21 2016 06:07:18 PM"))
colnames(df) <- c("Date") # Set Column name
dt <- setDT(df) # Convert to data.table
ff <- function(x) as.POSIXlt(strptime(x,"%B %d %Y %H:%M:%S %p"))
dt[,dates := as.Date(ff(Date))]
When I try creating a new variable called TOD, I get the output in H:M:S format without converting it into 24 hour format. What I mean is that for the 3rd row, instead of getting 21:50:49 I get 09:50:49. I tried two different ways to do this. One use as.ITime from data.table and then also using strptime. The code I use to calculate TOD is below.
dt[,TOD1 := as.ITime(ff(Date))]
dt$TOD2 <- format(strptime(dt$Date, "%B %d %Y %H:%M:%S %p"), format="%I:%M:%S")
I thought of trying it using dataframe instead of data.table to eliminate any issues with using strptime in data.table and still got the same answer.
df$TOD <- format(strptime(df$Date, "%B %d %Y %H:%M:%S %p"), format="%I:%M:%S") # Using dataframe instead of data.table
Any insights on how to get the right answer?
As commented #lmo, you need to use %I parameter instead of %H, from ?strptime:
%H
Hours as decimal number (00–23). As a special exception strings
such as 24:00:00 are accepted for input, since ISO 8601 allows these.
%I
Hours as decimal number (01–12).
strptime("April 21 2016 09:50:49 PM", "%B %d %Y %I:%M:%S %p")
# [1] "2016-04-21 21:50:49 EDT"
Here you go:
library(lubridate)
df$Date <- mdy_hms(df$Date)
Note that while mdy_hms is extremely convenient and takes care of the 12 / 24 hour time for you, it will automatically assign UTC as a time zone. You can specify a different one if you need. You can then convert df to a data.table if you like.