Convert time AM and PM TO 24 hour format - flutter

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

Related

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)

How to convert unix timestamp to iso 8601 in Flutter

I am getting date from a server as a unix timestamp, how can I convert it to ISO 8601 date format in flutter?
the date I receive:
1611694800000
How I want to convert it to be
2021-01-26T22:00:00.000+00:00
What I have done so far with no luck
String s = '1611694800000';
debugPrint("Recevied date is: $s");
String dateS = DateTime.parse(s).toIso8601String();
debugPrint("Converted date : $dateS");
String dateStr = (dateS.split(".")[0].split("T")[0] + " 00:00:00").substring(1);
debugPrint("Activation date: $dateStr");
I end up getting:
Unhandled Exception: FormatException: Invalid date format.
Use DateTime.fromMillisecondsSinceEpoch:
var timestampMilliseconds = 1611694800000;
var datetime =
DateTime.fromMillisecondsSinceEpoch(timestampMilliseconds, isUtc: true);
print(datetime.toIso8601String()); // Prints: 2021-01-26T21:00:00.000Z
(Note that the printed time is one hour off of your stated expectation, but I'm assuming that's a mistake in your expectation.)
The reason why you are getting invalid date format is because you have to provide date in string like '2021-04-19' and not milliseconds;
This package makes it easy to format dates time_formatter

how to convert a String to a Date without changing the Format

I'm using Xcode 11.4.1
In my Project I got a String with some data which I need to split up and save the Information in some variables.
now I face a problem if I try this
let dateString = "12-04-2020"
let dateFormatIn = DateFormatter()
dateFormatIn.dateFormat = "dd-MM-yyyy"
let saveDate: Date = dateFormatIn.date(from: dateString)!
print("The date is: \(saveDate)")
the result which I expected is "The date is: 12-04-2019" but what I got is "The date is: 2020-04-11 15:00:00 +0000"
what do I miss, what is this printed Date? it's not the current date ans also not the String!
I need the Date in the same format as I got it in the String.
A Date is a point in time, it doesn't have a format nor a time zone.
print shows the description – a string representation – of the date in the UTC time zone.
Your time zone is obviously UTC +0900, 2020-04-12 00:00:00 +0900 and 2020-04-11 15:00:00 +0000 is the same point in time.
If you want to create the date string independent of the time zone add the line
dateFormatIn.timeZone = TimeZone(secondsFromGMT: 0)
let saveDate: Date = dateFormatIn.date(from: dateString)!
The ": Date" looks like a data type declaration. You're saying, "create a variable called saveDate of type Date, and initialize it from the output of dateFormatIn.date(from: dateString)".
So you're not printing the formatted output. You're converting the formatted output to a new Date object, and then printing that.
Try:
let saveDate = dateFormatIn.date(from: dateString)!
This should save whatever dateFormatIn.date(from: dateString) returns in saveDate.

Convert mach absolute timestamp to NSDate

I have a mach absolute timestamp from a sql database, I want to read in my app. I want to display this timestamp, so I want to convert it to NSDate. How is this possible? I have a timestamp like this: 443959550 (result in this case is UTC: 26.01.2015 10:05:50)
443959550 seems to be a time interval since the reference date
1 January 2001, GMT:
let date = NSDate(timeIntervalSinceReferenceDate: 443959550)
println(date) // 2015-01-26 10:05:50 +0000
Take a look at timeIntervalSince1970. There you can set a timestamp as init-parameter.
var timeStamp = 443959550
//NSDate
var date = NSDate(timeIntervalSince1970:443959550)

Convert Long date to specific short date format

Convert Long date format to specific short date format.
I want to get the Date from Datepicker(Jcalander) , format to dd-mm-yyyy format and assign to String variable. I tried using codes shown below. But didnt get the date format i want.
SimpleDateFormat simpleFormat = (SimpleDateFormat) jCalendarCombo1.getDateFormat();
Date date = jCalendarCombo1.getDate();
System.out.println(date); // Prints Thu Mar 28 00:00:00 IST 2013
String s = simpleFormat.format(date);
System.out.println(s); // prints Thursday, March 28, 2013
System.out.println("Date SHORT format: " + DateFormat.getDateInstance(DateFormat.SHORT).format(date)); // prints 3/28/13
If you want a fixed, non locale dependent format, you can just create it yourself;
SimpleDateFormat shortformat = new SimpleDateFormat("dd-MM-yyyy");
String s = shortformat.format(date);
System.out.println(s); // Prints 29-03-2013