Revert datetime format, from short to long - flutter

Tried searching around couldn't really find anything. Was hoping to find a way to revert the datetime format.
So I start off with: 4-11-22 and I want to change to Friday, 12 November 2022.
Using the intl package

import intl package
import 'package:intl/intl.dart';
Then, you can do as follows:
String myDate = '4-11-22'; // input date
String pattern = 'dd-MM-yy'; // define parse pattern for the input date
DateTime date = DateFormat(pattern).parse(myDate); // parse the input date
String newPattern = 'EEEE, dd MMMM yyyy'; // define new pattern
String formattedDate = DateFormat(newPattern).format(date); // reformat
print(formattedDate); // result: Friday, 04 November 2022
Try on DartPad
For more formatting possibilities, go to the docs.

final oldDateDateTime = DateFormat('dd-MM-yy').parse('4-11-22');
final newDateString = DateFormat('EEEE, d MMMM y', 'en_US').format(oldDateDateTime);
print(oldDateDateTime.toString());
print(newDateString);
Output:
2022-11-04 00:00:00.000
Friday, 4 November 2022
More in: https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html

You can do as follows
final DateFormat formatter = DateFormat('EEEE, dd MMMM yyyy');
final String formatted = formatter.format(DateTime.now());
Then you use the formatted

Related

Flutter custom date format

I am facing issue while formatting the date to custom format.
I need to convert date yyyy-MM-dd HH:mm:ss ===> EEEE, MMM dd, yyyy
For example I am getting date from server 27-10-2022 11:02:50, and I need to convert it to Thursday, October 27, 2022
Getting Date format is "dd-MM-yyyy HH:mm:ss" and the desire format will be "EEEE, MMMM dd, yyyy"
final data = "27-10-2022 11:02:50";
final format = DateFormat("dd-MM-yyyy HH:mm:ss");
final DateTime result = format.parse(data);
print(result); //2022-10-27 11:02:50.000
final newFormatter = DateFormat("EEEE, MMMM dd, yyyy");
final newFormatString = newFormatter.format(result);
print(newFormatString); // Thursday, October 27, 2022
I am using intl package
Just checked in flutter docs. Your date format is not good. For converting string to date.
The following date format is required,
"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456789z"
"2012-02-27 13:27:00,123456789z"
"20120227 13:27:00"
"20120227T132700"
"20120227"
"+20120227"
"2012-02-27T14Z"
"2012-02-27T14+00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"
And you can convert that into the format like below
var date1 = DateFormat('dd-MM-yyyy hh:mm:ss').parse("27-10-2022 11:02:50");
var date2 = DateFormat('yyyy-MM-dd hh:mm:ss').format(date1);
print( DateFormat('EEEE, MMMM dd, yyyy').format(date2));
please try this, hope you will get the idea,
print(DateFormat('dd-MM-yyyy HH:mm:ss').parse('27-10-2022 11:02:50'));

Google Scripts Timestamp - Format Output on Email

I'm not sure what I'm doing wrong here, and this is probably really simple... After scouring the net and trying hundreds of different examples found, I've come up empty handed on getting the format needed. My script works, sends the email, all values are there, but the date formats are not what we are after. So, here is what's up:
I've got a basic script that sends an HTML email from a template when a respondent submits a form. The timestamp, start (date and time), end (time only) value is needed, and is printed in the HTML email, but it's showing a full-blown timestamp output such as: "Tue Nov 03 2020 11:39:28 GMT-0700 (Mountain Standard Time)"
What I am trying to do is format the timestamp value shown in the email to this: "Tue Nov 03 2020 HH:mm"
Here is the script I am using:
function onFormSubmit(e) {
var htmlBody = HtmlService.createTemplateFromFile('email');
var rng = SpreadsheetApp.getActiveSheet().getActiveRange();
var timestamp = Utilities.formatDate(new Date(), "MST" , "MM-dd-yyyy | HH:mm:ss");
var email = rng.getValues()[0];
var body = HtmlService.createTemplateFromFile("email");
var to = 'foo#bar';
var subject = 'Activity Report ' + email[0] + '';
htmlBody.timestamp = email[0];
htmlBody.start = email[1];
htmlBody.end = email[2];
htmlBody.activityobserved = email[3];
htmlBody.summary = email[4];
htmlBody.actiontaken = email[5];
htmlBody.attachments = email[6];
var email_html = htmlBody.evaluate().getContent();
MailApp.sendEmail({
to: to,
subject: subject,
htmlBody: email_html,
replyTo:'bar#foo',
});
}
Don't forget to actually use timestamp. To get the "Tue" part in your date, you can use "EEE". I set the date formatting below as you specified in your question.
function onFormSubmit(e) {
// ...
var timestamp = Utilities.formatDate(new Date(), "MST" , "EEE MMM dd yyyy HH:mm");
// ...
htmlBody.timestamp = timestamp;
htmlBody.start = Utilities.formatDate(email[1], "MST" , "EEE MMM dd yyyy HH:mm");
htmlBody.end = Utilities.formatDate(email[2], "MST" , "EEE MMM dd yyyy HH:mm");
// ...
}
Solution:
You don't include variable timestamp in the htmlBody object. Instead you are using the original source value of it.
Replace:
htmlBody.timestamp = email[0];
with:
htmlBody.timestamp = timestamp;
Update based on your comment:
Im a little confused on how to format the start and end times though.
They are still displaying the full output.
Assuming that you have date objects in your sheet,
Replace:
htmlBody.start = email[1];
htmlBody.end = email[2];
with
htmlBody.start = Utilities.formatDate(new Date(email[1]), "MST" , "EEE MMM dd yyyy HH:mm");
htmlBody.end = Utilities.formatDate(new Date(email[2]), "MST" , "EEE MMM dd yyyy HH:mm");

JavaFX8 Convert String to date using DateTimeStringConverter

I would like convert string to date (dd/MM/YYYY) - this is useful for compare dates in TableColumns.
so i use DateTimeStringConverter (the string "01/11/2014" is a value of DatePicker)
DateTimeStringConverter format = new DateTimeStringConverter(Locale.FRANCE,
"dd/MM/YYYY");
Date d1 = format.fromString("01/11/2014");
d1.toString()
I don't obtain the right date but this date = "Mon Dec 30 00:00:00 CET 2013" !!!
I don't understand what is the problem (which in fact should not be a problem) ?
Any ideas ?
Thank you in advance
Fabrice

ParseException: Unparseable date: "04 December"

I have the birthday date 04 December I want to save it as 04-12 in the database, for that I do this:
val birthday = theForm.field("birthday") //String
val date = new java.text.SimpleDateFormat("dd-mm", Locale.ENGLISH).parse(birthday)
But i get the error: ParseException: Unparseable date: "04 December"
Any idea? Thanks!
Your date format string is not correct. Try it as "dd MMMM". The javadocs for SimpleDateFormat are pretty comprehensive for format possibilities:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
It seems like you want to parse it as one format and then re-format it into another. For this you can use two separate SimpleDateFormat instances, one with "dd MMMM" for parsing the 04 December format and one with "dd-MM" for re-formatting into the format you want to save to your db. The code would look like this:
val date = new SimpleDateFormat("dd MMMM", Locale.ENGLISH).parse(birthday)
val dbDateString = new SimpleDateFormat("dd-MM", Locale.ENGLISH).format(date)
What about:
import java.time._
val birthDay = MonthDay.parse("--12-04")

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