Why "Invalid date format"? [closed] - dart

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I cannot understand why the date format is invalid
I format the date as I want to display it in my field
_dr = formatDate(DateTime.fromMillisecondsSinceEpoch(Tools.birthDate),
[dd, '.', mm, '.', yyyy]);
_birthDateController = new TextEditingController(text: _dr);
Trying to convert it to Date
_dateTime = DateTime.parse(_birthDateController.text);

It is not clear from your shared code what formatDate method is doing behind the scenes. However, for effectively parsing and formatting the date and time, you can use the DateFormat class that is part of the intl package
Sample code:
import 'package:intl/intl.dart';
main() {
final date = DateTime.now();
final dateFormat = 'dd.MM.yyyy';
final formattedDate = DateFormat(dateFormat).format(date);
print('formatted date: $formattedDate');
final parseFormattedDate = DateFormat(dateFormat).parse(formattedDate).toIso8601String();
print('parse formatted date: $parseFormattedDate');
}
Output:
formatted date: 18.09.2020
parse formatted_date: 2020-09-18T00:00:00.000
for a list of available date and time formats see the documentation for the DateFormat class

Related

How to modify a date to meet correct groovy standard?

I am trying to parse the following date in Jenkins 2021-10-14T18:12:20.578+00:00 however, I am getting the error Unparseable date: "2020-01-01T10:10:20.578+00:00"
This is my code, not sure what I am doing wrong:
Date myDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2020-01-01T10:10:20.578+00:00");
EDIT:
Thanks to Kaus, I found that my date is not formatted properly and should be 2020-01-01T10:10:20.578GMT+00:00
I'm getting this date from some other files. I can replace + with GMT+ as follow:
def myDate = "2020-01-01T10:10:20.578+00:00"
myDate = myDate.replaceAll("\\+", "GMT\\+")
How can I do the same thing if my date is "2020-01-01T10:10:20.578-06:00"
The following is replacing every "-"
def myDate = "2020-01-01T10:10:20.578-06:00"
myDate = myDate.replaceAll("\\+", "GMT\\+").replaceAll("\\-", "GMT\\-")
Output: "2020GMT-01GMT-01T10:10:20.578GMT-06:00"
Use X for ISO8601 time zone, instead of Z for RFC 822 time zone.
(from https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
import java.text.SimpleDateFormat
Date myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.parse("2020-01-01T10:10:20.578+00:00")
Missing GMT there
Date myDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2020-01-01T10:10:20.578GMT+00:00");

SwiftUI custom number formatter as phone

I'm trying to create a custom phone formatter. I found PhoneNumberKit that could work for my particular case, the problem with this is that when I tried it there are multiple issues like:
If using .international the program enters in a loop and becomes irresponsive
It requires the number to contain the country's phone-code inside the text to be able to format it correctly, but as I have already done this part in a Button with a flag + phone code on the left, it's not necessary.
So, what I'm trying to do is to create my own PhoneFormatter, something like: (###) ###-####, currently this might suffice as we're only targeting a single country and this is the most common format used here.
I'm following the code from this site to get an idea of how to do it, but here they are working with dates:
struct ContentView: View {
static let taskDateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .long
return formatter
}()
var dueDate = Date()
var body: some View {
Text("Task due date: \(dueDate, formatter: Self.taskDateFormat)")
}
}
Someone asked something similar 2 years ago on Apple's Developer Forums but I believe it was for UIKit rather than SwiftUI and there's no code shown there.
I'm trying to attach my formatter directly to my own TextField, I've tried looking for something similar with SwiftUI and I haven't found anything, and still can't find myself how to create my own custom formatter using something like formatter.numberStyle = "(###) ###-####"
Could anyone point me in the right direction?

Convert string to Date in ios by cordova problem

I want to convert this string "2018-11-13T9:24" to Date in my js Code
var date = new Date('2018-11-13T9:24');
app.dialog.alert(date, 'Date');
but in my alert, I see:
I don't have any problem with time after 10 like '2018-11-13T11:24'

Grails databinding date format

I have a problem within a grails 2.3 application, when it comes to data binding and correct date formats.
I use a datepicker (jQuery ui) that provides a <input type="hidden" /> that holds the selected date in ISO_8601 format. It will post a value like this: 2015-08-14 to the controller. The form itself and the post result is correct.
I use this simplified model:
class Thing {
DateTime lastUpdated
static constraints = {
lastUpdated nullable: true
}
}
when I try to create an entity, I will face this error message:
Invalid format: "2015-08-14" is malformed at "15-08-14"
A first research lead me to this change in the Config.groovy:
jodatime.format.html5 = true
(Link 3 in the list below)
Appying this leads to change. Now the error message is:
Invalid format: "2015-08-14" is too short (flip table)
Another try was to change the databinding.dateFormats to this (also in the Config.groovy):
grails.databinding.dateFormats = [ "yyyy-MM-dd HH:mm:ss.S","yyyy-MM-dd'T'hh:mm:ss'Z'", "yyyy-MM-dd"]
Which has no effect what so ever.
For my understanding a given date format should automatically be marshaled in a dateTime object. What configuration did I miss?
Here are relative question, that sadly did not help me:
bind date to command object in Grails
GORM default Date format when sending date to Grails
Grails unable to unmarshall date/time from JSON back into joda DateTime
You should add next line in config.groovy
jodatime { format.org.joda.time.DateTime = "yyyy-MM-dd" }
But if you don't need time in this field, it's better to use LocalDate instead of DateTime here.
class Thing {
LocalDate lastUpdated;
...
jodatime {
format.org.joda.time.DateTime = "yyyy-MM-dd HH:mm:ss"
format.org.joda.time.LocalDate = "yyyy-MM-dd"
}
So you will use DateTime where you need date with time and LocalDate where date is enough

Groovy joda-time convert time

In my domain I have
Time startTime
Time endTime
In my controller I need to covert the time from the view which is in a format of HH:MM to the acceptable format to submit to the domain. I have installed the plugin Joda-Time but I've come a bit stuck.
def startTime = params.startTime
def fmt_in = DateTimeFormat.forPattern("HH:mm:ss")
def fmt_out = ISODateTimeFormat.dateTime()
println fmt_out.print(fmt_in.parseDateTime(startTime))
sorry, newbie to groovy grails
After parsing for DateTime, you need to transform it to the desired type. For date and time without considering timezone I suggest you to use LocalDateTime and LocalTime.
def formatter = DateTimeFormat.forPattern("HH:mm:ss")
LocalTime time = formatter.parseLocalDateTime(params.startTime).toLocalTime()

Resources