I am trying to convert a Date String of format
Mon Dec 03 2018 05:30:00 GMT+0530 (India Standard Time)
into a format of YYYY-MM-DD so as to use it for a value in input date but it's not able to parse it.
I tried using these formats but it didn't work.
moment(new Date(Mon Dec 03 2018 05:30:00 GMT+0530 (India Standard Time))).format("YYYY-MM-DD")
But since it's not a Date.parse() format, it doesn't work.
Any ideas would be welcome.
You need to pass the dateFormat while creating the moment object, inorder for it to recognize and parse non-default formats.
Try,
var dateFormat = "ddd MMM DD YYYY HH:mm:ss zZZ";
var m = moment("Mon Dec 03 2018 05:30:00 GMT+0530 (India Standard Time)", dateFormat);
m.format("YYYY-MM-DD");
More details for parsing from string: https://momentjs.com/docs/#/parsing/string-format/
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 need to convert a date format
2020-07-28T21:00:00(RAML date-time only format)
to
Tue, 28 jul 2020 08:49:37 GMT format.
how to achieve this in mule-4?
%dw 2.0
output application/java
---
"2020-07-28T21:00:00" as DateTime
as String {format:"eee, dd MMM yyyy HH:mm:ss"}
https://simpleflatservice.com/mule4/Date_format.html
To format a date it’s necessary to first coerce to a Date format then format it to the desired Date format as did below
%dw 2.0
output application/java
---
"2020-07-28T21:00:00" as DateTime
as String {format:"eee, dd MMM yyyy HH:mm:ss"}
o/p of this will be :
Tue, 28 Jul 2020 21:00:00
here addition to this I would like to say that this is the default GMT format
but if you want to convert any date format for example GMT to IST then you have to do as in the below code
%dw 2.0
output application/java
---
(now() as DateTime >> "IST")
as String {format:"eee, dd MMM yyyy HH:mm:ss"}
in above code at the time when I am answering this question now() is giving the time as
2021-02-03T07:08:37.002Z[GMT] but I want my answer in IST so I changed it first to IST and then in the desired format.
so final o/p of this will be in IST as below:
Wed, 03 Feb 2021 12:42:59
I have data imported from an external report which contains a column of dates in the following format : 04 Nov 2018 07:39 AM
I would like to parse that string into a date but am unable to find an orders string that can handle that format
library(lubridate)
Vec1 <- ("04 Nov 2018 07:39 AM")
parse_date_time(Vec1, orders = 'dmy')
[1] NA
Warning message:
All formats failed to parse. No formats found.
Read ?strptime
Vec1 <- ("04 Nov 2018 07:39 AM")
as.POSIXct(Vec1, format = "%d %b %Y %H:%M %p")
#[1] "2018-11-04 07:39:00 GMT"
With lubridate you actually needed the hour and minute component
library(lubridate)
parse_date_time(Vec1, orders = "dmyhm")
Or as #ed_sans mentioned
dmy_hm(Vec1)
Currently, my dataset has a time variable (factor) in the following format:
weekday month day hour min seconds +0000 year
I don't know what the "+0000" field is but all observations have this. For example:
"Tues Feb 02 11:05:21 +0000 2018"
"Mon Jun 12 06:21:50 +0000 2017"
"Wed Aug 01 11:24:08 +0000 2018"
I want to convert these values to POSIXlt or POSIXct objects(year-month-day hour:min:sec) and make them numeric. Currently, using as.numeric(as.character(time-variable)) outputs incorrect values.
Thank you for the great responses! I really appreciate a lot.
Not sure how to reproduce the transition from factor to char, but starting from that this code should work:
t <- unlist(strsplit(as.character("Tues Feb 02 11:05:21 +0000 2018")," "))
strptime(paste(t[6],t[2],t[3], t[4]),format='%Y %b %d %H:%M:%S')
PS: More on date formats and conversion: https://www.stat.berkeley.edu/~s133/dates.html
For this problem you can get by without using lubridate. First, to extract individual dates we can use regmatches and gregexpr:
date_char <- 'Tue Feb 02 11:05:21 +0000 2018 Mon Jun 12 06:21:50 +0000 2017'
ptrn <- '([[:alpha:]]{3} [[:alpha:]]{3} [[:digit:]]{2} [[:digit:]]{2}\\:[[:digit:]]{2}\\:[[:digit:]]{2} \\+[[:digit:]]{4} [[:digit:]]{4})'
date_vec <- unlist( regmatches(date_char, gregexpr(ptrn, date_char)))
> date_vec
[1] "Tue Feb 02 11:05:21 +0000 2018" "Mon Jun 12 06:21:50 +0000 2017"
You can learn more about regular expressions here.
In the above example +0000 field is the UTC offset in hours e.g. it would be -0500 for EST timezone. To convert to R date-time object:
> as.POSIXct(date_vec, format = '%a %b %d %H:%M:%S %z %Y', tz = 'UTC')
[1] "2018-02-02 11:05:21 UTC" "2017-06-12 06:21:50 UTC"
which is the desired output. The formats can be found here or you can use lubridate::guess_formats(). If you don't specify the tz, you'll get the output in your system's time zone (e.g. for me that would be EST). Since the offset is specified in the format, R correctly carries out the conversion.
To get numeric values, the following works:
> as.numeric(as.POSIXct(date_vec, format = '%a %b %d %H:%M:%S %z %Y', tz = 'UTC'))
[1] 1517569521 1497248510
Note: this is based on uniform string structure. In the OP there was Tues instead of Tue which wouldn't work. The above example is based on the three-letter abbreviation which is the standard reporting format.
If however, your data is a mix of different formats, you'd have to extract individual time strings (customized regexes, of course), then use lubridate::guess_formats() to get the formats and then use those to carry out the conversion.
Hope this is helpful!!
I have a set of date times in my DB, generated by Javascript new Date(). in R, I read them into data frames as character, thereafter I need to convert them to POSIXct date times, but any combination of format that I try returns NA, any idea how to fix it? thanks
As an example, here is some of data I have:
> datetimes = c("Thu Dec 01 2016 14:53:38 GMT+0100 (CET)", "Thu Dec 01 2016 14:54:38 GMT+0100 (CET)", "Thu Dec 01 2016 14:55:38 GMT+0100 (CET)")
> class(datetimes)
[1] "character"
> c_datetimes = strptime(datetimes, format = '%a %b %d %Y %H:%M:%S')
> c_datetimes
[1] NA NA NA
First, the database should store actual datetime values, not strings. If this can't be fixed, the code that generates the data should be modified to return ISO8601 strings. Just call Date.toJSON() or the identical toISOString() to get a string in the ISO8601 form: 2017-02-14T12:55:58.376Z.
As the name implies, Json dates are in this format. All REST APIs expect such a parameter too. Anything else simply covers up the problem.
The reason you can't parse the current text is that you are probably in a non-English locale. You can disable localized parsing by setting LC_TIME to C.
Once you do that, you can parse the text wtih the '%a %b %d %Y %H:%M:%S GMT%z' format string. Note GMT and %z. The GMT literal ensures that GMT is ignored in the string. %z will parse the offset.
The snippet:
datetimes = c("Thu Dec 01 2016 14:53:38 GMT+0100 (CET)",
"Thu Dec 01 2016 14:54:38 GMT+0100 (CET)",
"Thu Dec 01 2016 14:55:38 GMT+0100 (CET)")
Sys.setlocale("LC_TIME", "C")
strptime(datetimes, format = '%a %b %d %Y %H:%M:%S GMT%z')
Will return :
[1] "2016-12-01 15:53:38" "2016-12-01 15:54:38" "2016-12-01 15:55:38"
You'll note that the offset was taken into account to generate the correct local time for my machine, which is at +2:00 during winter.
UPDATE
Both toJSON() and toISOString() return UTC time. If you want to preserve the offset information and the data was generated using Javascript you may have to use moment.js to generate strings with the offset, as shown here :
var m = moment(); // get "now" as a moment
var s = m.format(); // the ISO format is the default so no parameters are needed
// sample output: 2013-07-01T17:55:13-07:00
Probably your locale settings are not English:
datetimes = c("Thu Dec 01 2016 14:53:38 GMT+0100 (CET)", "Thu Dec 01 2016 14:54:38 GMT+0100 (CET)", "Thu Dec 01 2016 14:55:38 GMT+0100 (CET)")
Sys.setlocale("LC_TIME", "C")
strptime(datetimes, format = '%a %b %d %Y %H:%M:%S GMT%z', tz = "GMT") #choose wichever timezone you like here
[1] "2016-12-01 13:53:38 GMT" "2016-12-01 13:54:38 GMT" "2016-12-01 13:55:38 GMT"