Dataweave Date Formatting with Timezone - date

I'm trying to format a date to the following format, UTC with the Timezone Offset:
Expected:
2021-01-20T21:00:00-06:00
Actual:
2021-01-20T00:00:00-06:00
Code:
var arrivedAt = "2021-01-20T15:00:00"
var timezone = "America/Chicago"
var finalDateTime = (((arrivedAt as Date) as String {format: "yyyy-MM-dd'T'00:00:00"}) as LocalDateTime ++ (timezone as String))
I'm assuming it's due to the "00:00:00" in the format but when I try using "HH" or "hh" I'm receiivng errors.

The good news is that it's documented here https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-change-time-zone
This example code assigns a timezone to the original date and then shift it to UTC
var arrivedAt = "2021-01-20T15:00:00"
var timezone = "America/Chicago"
var dateWithTimezone = (arrivedAt as LocalDateTime) ++ "America/Chicago"
// dateWithTimezone = "2021-01-20T15:00:00-06:00"
var dateOnUTC = dateWithTimezone >> "UTC"
// dateOnUTC = "2021-01-20T21:00:00Z"
---
dateOnUTC as String { format: "yyyy-MM-dd'T'HH:mm:ssXXX"}
NOTE: I'm not sure if the expected value is right on the question or if the example date is wrong.

It looks like you are using the incorrect data types and trying to do the date transformations as strings. That is highly unadvised. The errors are probably because a Date doesn't has a time neither a timezone.
One alternative is to treat the date time as a DateTime, to be able to use the timezone features:
%dw 2.0
output application/json
var arrivedAt = "2021-01-20T15:00:00"
var timezone = "America/Chicago"
var finalDateTime = (arrivedAt as DateTime >> timezone ) as String {format: "yyyy-MM-dd'T'HH:mm:ssZ"}
---
finalDateTime
Output
"2021-01-20T09:00:00-0600"
Actually the correct hour seems 9 instead of 21 as in your expected result. Maybe you did the timezone transformation the other way around?

Related

Change this "2022-07-26T12:10:07.000+0000" format to DateTime with 12 hrs format? Flutter

How i can extract the Time alone in this format "2022-07-26T12:10:07.000+0000" and show in 12hrs.
The output i am supposed to get is "5:40 PM", but i am getting 12.10 AM.
How to get exact time in the above mentioned format?
This is the method i followed to parse the DateTime.
String getTimeStringNew(String date) {
DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
var dateValue = dateFormat.parse(date);
DateFormat dateFormat1 = DateFormat("hh:mm a");
var value = dateFormat1.format(dateValue);
return value;}
In this method i am getting "12:10 AM".
But correct time is "5.40 PM".
What is the mistake i am doing here. Please correct.
2022-07-26T12:10:07.000+0000
Here 2022 is year
07 is month
26 is day
12 is hour
10 is minutes
07 is seconds.
So you will get 12.10 only
Maybe you are recieving the time in UTC.
You can use .toLocal to convert it to local time
var dateLocal = dateUtc.toLocal();
Edit
String getTimeStringNew(String date) {
DateTime _localDate = DateTime.parse(date).toLocal();
DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
var dateValue = dateFormat.format(_localDate);
DateFormat dateFormat1 = DateFormat("hh:mm a");
var value = dateFormat1.format(_localDate);
return value;}

Read timestamp field and print as 12 December 2017 2:34:23

Initially, I had used string format for the field timestamp and I was able to convert it to 12 December 2017 2:34:23 format using toData().toString(). I need to do the same thing when the field timestamp is in timestamp format. Can I get some help on this?
You can get a DateTime variable from a timestamp using
var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp);
Once you have your date you can format however you want using a DateFormat for example:
var formatter = new DateFormat('yyyy-MM-dd'); // You can change the format here
String formatted = formatter.format(date);
print(formatted); // Something like 2019-04-20
just check out this example below :
var timestamp =1583772663;
var format = new DateFormat('dd MMMM yyyy, hh:mm:ss');
var date = new DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
var value = format.format(date);
print(value);
//output : 09 March 2020, 10:21:03
let me know if it works.

Mongodb insertOne number type and date type without aggregation

this my script
import moment from 'moment';
var date = moment().format();
var value = request.query.value;
collection.insertOne({value: value, date: date});
How to convert value to int and date to date
Thanks
Guess that's what you're looking for :
collection.insertOne({value: parseInt(value), date: moment().toDate()});
Can't you just do this?
import moment from 'moment';
var date = moment().format().toDate();
var value = parseInt(request.query.value);
collection.insertOne({value: value, date: date});
plus don't use var in TypeScript, they introduced let for good reasons,

can't convert time stamp date to string in swift 4

I'm trying to convert a timeStamp string date to Date.
The result always returns nil.
func getDatefromTimeStamp (str_date : String , strdateFormat: String) -> String {
// stringDate '2018-01-01T00:00:00.000+03:00'
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) as TimeZone!
let date = dateFormatter.date(from: str_date)
dateFormatter.dateFormat = strdateFormat
let datestr = dateFormatter.string(from: date!)
return datestr
}
Your primary issue is that the format "yyyy-MM-dd'T'HH:mm:ssZ" does not match a string such as "2018-01-01T00:00:00.000+03:00". That string contains milliseconds but your format doesn't.
Update your format to "yyyy-MM-dd'T'HH:mm:ss.SSSZ".
That will fix the nil result.
Then you should clean-up your use of NSTimeZone. Just use TimeZone.
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
But there is no need to set the timezone when parsing this string because the string includes timezone information.
However, you may or may not want a timezone set when converting the resulting Date into the new String. It depends on what result you want.
Do you want the final string in UTC time (which is what you will get with your current code) or do you want the final string in the user's local time?
If you want the final string in the user's local time, don't set the timezone property at all. It will default to local time.
First of i highly recommend that you use guards instead of forces
Secondly why are you setting the date format twice? the first time is dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" then a few lines later resetting it with the parameter you passed in dateFormatter.dateFormat = strdateFormat. pick one and set it in the beginning - that may be causing your problem
Thirdly if that above is not the problem - make sure that your date is exactly in the necessary format if it is at all wrong it will return nil. even spaces and colons have to be perfect, i suggest using string builder to make sure they are consistant

convert string to datetime and compare for the 2 date diff

Question 1: I have 2 fields to let user enter start date and end date, but in string format
- DateStart (string: yyyy/mm/dd hh:mm)
- DateEnd (string: yyyy/mm/dd hh:mm)
May I how to compare both datetime? I want to know total how many hours is difference between the both date.
Question 2: user will enter 1 returnDate (string: yyyy/mm/dd hh:mm) also in string format, may I know how to update the returnDate if I will need to add 55hours on the returnDate?
Thanks
Start by taking a look at SimpleDateFormat, which will allow you to convert the String value to a Date object.
For example...
try {
// Note hh is Hour in am/pm (1-12), based on you example, it's not possible
// now the day part (ie am or pm), you could supply aa as the am/pm marker
// or use HH which is Hour in day (0-23)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm");
Date date = sdf.parse("2014/04/11 4:46");
System.out.println(date);
} catch (ParseException exp) {
exp.printStackTrace();
}
Then you can use JodaTime to calculate the difference between the two dates, see How to find difference between two Joda-Time DateTimes in minutes for an example
It should be noted that you could skip the use of SimpleDateFormat and JodaTime all the way, check out String to joda LocalDate in format of "dd-MMM-yy" for an example of converting a String to a LocalDate using JodaTime
To add time to an existing Date, you can use either Calendar or JodaTime, see how to add days to java simple date format for an example of both
I would recommend that if you are using some kind of GUI, you might consider using one of the available date pickers as it will save you a lot of hassel
I suggest using Java8's java.util.time package
Example:
public static void main(String[] args) {
// example input
String dateString1 = "2014/04/10 00:00";
String dateString2 = "2014/04/11 23:59";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
LocalDateTime date1 = LocalDateTime.parse(dateString1, dtf);
LocalDateTime date2 = LocalDateTime.parse(dateString2, dtf);
// do your stuff with the dates...
}
Here is my solution:
String date1 = "2014/04/10 15:30";
String date2 = "2014/04/11 09:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
try {
Date parsedDate1 = sdf.parse(date1);
Date parsedDate2 = sdf.parse(date2);
double secs = (parsedDate2.getTime() - parsedDate1.getTime()) / 1000;
double hours = secs / 3600;
System.out.println(hours);
} catch (ParseException e) {
e.printStackTrace();
}
For adding hours to a date:
Date date = new Date(someDateObject.getTime() + 55 * 3600 * 1000);