How to get local UTC time Flutter - flutter

You need to translate the time (UTC + 7) into the time that the user has on the phone.
My code:
String date = '20211231031500';
var dateTime = DateFormat("yyyyMMddHHmmss").parse(date, true);
dateLocal = dateTime.toLocal();
Error: Trying to read MM from 20211231031500 at position 14

Related

TemporalDate from DateTime - Flutter

I am using AWS amplify for my project and trying to save the selected date from datepicker to the model.
DatePicker is giving the date in DateTime object, when i try to convert it to TemporalDate its giving me a day before date. i.e if i select todays date and assigning it to the TemporalDate then i am getting yesterday's date.
Here is my code :
var d1 = TemporalDate(pickedDate);
var d4 = TemporalDate(pickedDate.toUtc());
var d2 = TemporalDate(pickedDate.toLocal());
var d3 = TemporalDate(pickedDate).getDateTime();
pickedDate is holding today's date but the d1,d2,d3,d4 all is having yesterday's date.
Expected result is to get the today's date in the TemporalDate variable.
You need to parse the date. It is showing a day before because it is on the universal date. So you just need to convert the universal date to local date.
var dateTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(selectedDate.toString(), true);
var dateLocal = dateTime.toLocal();

How to convert correctly only time in dart

i've a string like this
String time = '13:00:00Z'; (Zulu time)
I want to convert it into a string in the correct local time.
For example if i execute the program, i want 15:00 as a result because 13:00 in my local time it's 15:00.
I get the error "FormatExeption: Invalid date format", whatever I put in the DateFormat
You can prepend current date to the time string and then use toLocal() to convert it to your local time:
DateTime date = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd').format(date);
String time = "13:00:00Z";
DateTime dt = DateTime.parse(formattedDate + "T" + time).toLocal();
String formattedTime = DateFormat('HH:mm').format(dt);
print(formattedTime);
(DateFormat is from the intl package)

Convert time AM and PM TO 24 hour format

String timer = "01:30 PM"
String = "12/10/2020"
DateTime fdate = DateFormat("yyyy-MM-dd").parse(dates);
DateTime ftime = DateFormat("hh:mm:ss").parse(timer);
I am getting the error while converting the string time into the Original time format. How to convert that time and date into the different format.
How to get the combination of date and time like 2020-10-12 13:30:00
Change ftime to :
DateTime ftime = DateFormat("HH:mm:ss").parse(timer);
Consider reading DateFormat Documentation for more information

Current date from BlackBerry

I am developing an application which needs the current date from the device, but it only needs day-of-week, month, and day-of-month, like Friday October 14
I have tried this code with Calendar. How do I convert Date to String? Is this possible to get date in this format?
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
System.out.println("Date" + c.getTime());
and my output : `Fri Oct 14 16:17:03 Asia/Calcutta 2011`
You could use SimpleDateFormat:
SimpleDateFormat fmt = new SimpleDateFormat("EEEE MMMM dd");
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
System.out.print("Date ");
System.out.println(fmt.format(c.getTime()));
If you need those values in different Strings it gets a little bit more complicated (using DateFormatSymbols to get the month/weekday names):
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
DateFormatSymbols dateSymbols = new DateFormatSymbols();
String[] monthsText = dateSymbols.getMonths();
String[] weekdaysText = dateSymbols.getWeekdays();
String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
String month = monthsText[c.get(Calendar.MONTH)];
String weekday = weekdaysText[c.get(Calendar.DAY_OF_WEEK)];
System.out.format("day: %s, month: %s, weekday: %s", day, month, weekday);

date format using Ajaxpro

I am using AjaxPro to retreive database, the date field return Sat Apr 3 00:00:00 UTC+0700 1982 i want to format it like "dd/mm/yyyy".
Assuming you are getting a DateTime value from the server, you can do something similar to this:
Visit http://www.ajaxpro.info/Examples/DataType/default.aspx
and run the following code in Firebug.
var result = AJAXDemo.Examples.DataType.Demo.GetDateTime(new Date()).value;
var year = result.getFullYear();
var month = result.getMonth() + 1;
var day = result.getDate();
var formattedDate = [day, month, year].join('/');
console.log(formattedDate);