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

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.

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;}

Date Time format MM/dd/yyyy, HH:mm:ss a to milliseconds in Flutter

I have code like below
var date = DateTime.fromMillisecondsSinceEpoch(timeStamp);//timeStamp = 1630506255982
var d12 = DateFormat('MM/dd/yyyy, HH:mm:ss a').format(date); // 12/31/2021, 10:10:10 PM
Now I want to convert the d12 string back to timeStamp "1630506255982"
How can I achieve this in flutter code?
Use it like this
var dtime = DateFormat('MM/dd/yyyy, HH:mm:ss a').parse(d12);
var inMilliseconds = dtime.millisecondsSinceEpoch
You have to first call this import:
import 'package:intl/intl.dart';
Then base on your code, you can simply add the function that will convert it back to integer.
var date = DateTime.fromMillisecondsSinceEpoch(1630506255982);//timeStamp = 1630506255982
var d12 = DateFormat('MM/dd/yyyy, HH:mm:ss a').format(date); // 12/31/2021, 10:10:10 PM
var _convertedDateBackToInt = (date.toUtc().millisecondsSinceEpoch ~/ 1000).toInt(); /// Converts back the base [DateTime] value to [int].
print(_convertedDateBackToInt); // 1630506255982

Changing Date and time Time formate from Json Response in dart?

Hello friend I am new in dart fetching data from Api now I want format this date time. In this way => Mar 20, 2021 11:36 am.. While json response coming this way =>":"2021-03-20 11:36:00
Here is my json full response file please check answer the question?
"message":"Data fetched successfully",
"data":[
{
"id":1,
"public_id":"ed0042ca-5a14-476b-9b65-66461efa81ae",
"title":"LIVE CONCERT",
"image":"https:\/\/livinghopemobile.com\/public\/storage\/event\/200321_113705_image.png",
"datetime":"2021-03-20 11:36:00", <=
"description":"LIVE CONCERT",
"created_at":"2021-03-20T06:37:05.000000Z",
"updated_at":"2021-03-29T07:18:50.000000Z"
}
]
}
String date = DateFormat("MMM dd, yyyy hh:mm a").format(DateTime.parse("2021-03-20 11:36:00"));
print(date); // =>Mar 20, 2021 11:36 AM
try out this
var date = "2021-03-24T08:57:47.812"
var dateTime = DateTime.parse("$date");
var stdTime = DateFormat('MMM d, yy hh:mm a').format(dateTime).toString();
Try this
var time = DateTime.parse(data[0]['datetime']);
And then you can look up here on the dates format list Here
var myTime = DateFormat('MMM d, yy hh:mm a').format(time);

Dataweave Date Formatting with Timezone

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?

Datetime without 00:00:00.000 for flutter

Why everytime I make a
var date = DateTime.parse(dob);
var formattedDate = '${date.year}${date.month}${date.day}';
DateTime.parse(formattedDate)
it will still retrieve as 1961-02-26 00:00:00.000? How do I get it to become 19610226 but still in datatype DateTime?
Use DateFormat Class from intl package to format your date
var date = DateTime.parse(dob);
var dateFormatter = new DateFormat('yyyy-MM-dd');
String formattedDate = dateFormatter.format(date);
print(formattedDate); // result 2020-03-24
for more details and more formatting options see this answer