How to convert a String to Datetime using Dataweave Mule - string

I have the following string 20170912113456 and i want to convert it to this format 2017/09/12 11:34:56 using Dataweave Mule.
Any ideas about how to do it ?

I would recommend first converting your input date string into a Dataweave localdatetime object. Then you can output the date object back to a string of any format.
%dw 1.0
%output application/json
---
formattedDate: flowVars.date as :localdatetime{format: "yyyyMMddHHmmss"} as :string{format:"yyyy/mm/dd HH:mm:ss"}
If you interested in learning the inner-working check out my converting string dates to Java Date Objects in Mule tutorial, https://youtu.be/XqI_Kii9RzA

You can try few things like formatting to data and then to string based on below example.
myDate: ((payload as :string) as :date {format: "yyyyMMdd"}) as :string {format: "MM-dd-yyyy"}
For a better understanding the Date Time operations please go through below link.
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#date-time-operations

Related

Convert string to date format in azure data factory using set variable

I have string date in set variable "20211222"
And I want to convert it into date like 2021-12-22.
And I have use this function in variable set dynamic content
#formatDateTime('20211222', 'yyyy-MM-dd')
But error occur In function 'formatDateTime', the value provided for date time string '20211222' was not valid. The datetime string must match ISO 8601 format
Is there any other function to convert this string "20211222" into date?
Actually the string '20211222' is already in the unambiguous format of YYYYMMDD and will always be interpreted this way. If you need to use this string input as a date, just do a cast:
SELECT CAST('20211222' AS date); -- 2021-12-22
If you wanted to formerly go from your text input YYYYMMDD to a text output of YYYY-MM-DD, then make a round trip:
SELECT CONVERT(varchar(10), CAST('20211222' AS date), 120);
Function formatDateTime expects "a string that contains the timestamp".
Example:
formatDateTime('03/15/2018 12:00:00', 'yyyy-MM-ddTHH:mm:ss')
You would have to manage to input in a timestamp format. The default format for the timestamp is "o" (yyyy-MM-ddTHH:mm:ss:fffffffK), which complies with ISO 8601 and preserves time zone information.
Please use the below logic:
#concat(substring(pipeline().parameters.Test,0,4 ),'-',substring(pipeline().parameters.Test,4,2),'-',substring(pipeline().parameters.Test,6,2))

MongoDB script to convert String "09-Jan-20" // (DD-MMM-YY) into ISO Date

I have multiple records in a collection and date is stored as String as "09-Jan-20". I need to convert this date into ISODate by script execute on MongoDB.
exp: "09-Jan-20" to ISODate("2020-01-09T00:00:00.000Z").
Anyone, please help.
Got the solution for this question.
We can use mongo's $convert to convert the string into date format.
Exp:
"$convert":{
input: "$dateField",
to: "date"
}
We can also find out the records where date is in string format by-
"$type":"string"
Refrence:- mongo type check and mongo convert

MOMENTJS TIME FORMATTING

I have this data :
time: "2020-12-17T13:06:13.144Z"
and then I want change this data, to be like this...
"17-12-2020 13:06:13"
but when I change with this code:
{moment(time).format("DD-MM-YYYY HH:mm:ss")}
MY OUTPUT IS NOW :
"17-12-2020 20:06:13"
MY OUTPUT EXPECTATION:
"17-12-2020 13:06:13"
How can I change this format so that it matches my expectation?
Try utc() method before format:
moment(time).utc().format("DD-MM-YYYY HH:mm:ss")
You can add custom string in moment format string with [:]
Try this
{moment(time).format("["]DD-MM-YYYY HH:mm:ss["]")}

Grails - converting a String to Date and then back to String

I've got this string 21-04-2016 for instance, that I'm trying to convert to a date. After that's done I need to flip it around, remove all the dashes and then convert it back to a string so that it looks like 20160421. I have found some code that I'll post below but I want to know if there is a simple way of doing this, maybe without having to convert the string to a date? Anyhow, here's my (broken) code:
String from = region.startDate //I get this string from a controller
Date fromDate = Date.parse('yyyy-MM-dd', from) //.parse is depricated apparently
println(from)
println(fromDate)
Here's what I get back (from the println):
Wed Jul 08 00:04:00 SAST 16
11-04-2016
No need to convert from String to Date in that case (you could use a SimpleDateFormat (or rather two) to do that, though); just use String and List operations from Java and Groovy:
from.split('-').reverse().join()

Converting string to timestamp?

I have a xml file in which date is represented as this: "2010-07-10T14:46:00.000Z"
Im parsing it and fetching it as string.
I need to convert it into timestamp or date format to store in db. How do i do it?
Please help.
Assuming that you use PHP:
You can use the function DateTime::createFromFormat(string $format, string $time)
try the following:
$timestamp = str_ireplace(array('T', 'Z'), '', "2010-07-10T14:46:00.000Z");
$datetime = DateTime::createFromFormat('j-m-d H:i:s.u ', $timestamp);
echo $date->getTimestamp();
Please see the documentation of DateTime::createFromFormat and date() for the correct format string.
generally you need to use strptime() related functions, that come from standard C library.

Resources