Doesn't meter what string I pass to parse I'm always getting Illegal Argument exception on GWT DateTimeFormat.parse method.
For instance, what is wrong on the following code line:?
Date date = DateTimeFormat.getFormat("MM-dd-YYYY").parse("10-10-2012");
I get:
java.lang.IllegalArgumentException: 10-10-2012
at com.google.gwt.i18n.shared.DateTimeFormat.parse
Instead of YYYY for the year in the format, you should use yyyy. The format you are specifiying would match a date that looks like: 10-10-YYYY (since Y isn't a special date format character).
Related
I could use some help with an issue I am facing in a Rails project. I am using the strptime function to return a Date object from an inputted string time and target format:
date = Date.strptime(date, '%Y-%m')
And it seems when I pass in a Date that doesn't match that pattern, i.e. 01-01-24, it does not throw an ArgumentError for me to catch and do anything with. It does catch an invalid input like this: 01-2024 though. Is there a certain kind of validation I can do here to catch this kind of error and check that the RegEx pattern matches?
Date#strptime format is using '%Y-%m' as [year]-[month] so in the case of '01-01-2024' it is seen as [01]-[01]-2024 and parsed to that date structure.
strptime does not respect %Y as having a minimum width so your current expression is essentially equivalent to /\A-?\d+-(?:1[0-2]|0?[1-9])/ (any number of digits followed by a hyphen followed by 1-12 optionally 0 padded)
'01-2024' only raises an error because 20 is not a valid month. For Example: '01-1299' would not raise an error.
Rather than relying on this naivety, you could validate your "date pattern" using a Regexp e.g.
date.match?(/\A\d{4}-(?:1[0-2]|0[1-9])\z/)
Using pry to investigate with:
[22] pry(main)> date = Date.strptime('calimero', '%Y-%m')
Date::Error: invalid date
from (pry):20:in `strptime'
it throws a Date::Error exception
In dart I want to do this:
var s = "2018-11-23T04:25:41.9241411Z"; // string comes from a json but represented here for simplicity like this
var d = DateTime.parse(s);
but it throws a null.
Dart can't seem to parse iso 8601 date time formats. I've found a custom serializer called "Iso8601DateTimeSerializer" but how do I add it to my flutter app?
links: https://reviewable.io/reviews/google/built_value.dart/429#-
The instructions here only indicate adding it to dart using "SerializersBuilder.add" but I'm a newbie and cant find out how?
link:
https://pub.dartlang.org/documentation/built_value/latest/iso_8601_date_time_serializer/Iso8601DateTimeSerializer-class.html
The problem is that Dart's DateTime.parse only accepts up to six digits of fractional seconds, and your input has seven.
... and then optionally a '.' followed by a one-to-six digit second fraction.
You can sanitize your input down to six digits using something like:
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp(r"(\.\d{6})\d+"), (m) => m[1]);
Maybe the parse function should just accept more digits, even if they don't affect the value.
Just to add to Irn's answer. You need to add some escapes for the regex to work properly.
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(\\.\\d{6})\\d+"), (m) => m[1]);
You need to add it to the serializers builder.
Example:
#SerializersFor(models)
final Serializers serializers = (_$serializers.toBuilder()
..addPlugin(StandardJsonPlugin())
..add(Iso8601DateTimeSerializer()))
.build();
How can I convert this format 2018-12-03T15:42:00Z to real readable yMd and time also? Please mention with example code.
DateTime has a parse method for that:
DateTime.parse('2018-12-03T15:42:00Z')
or tryParse if you don't want an exception when the string is not in the correct format
DateTime.tryParse('2018-12-03T15:42:00Z')
I need to convert this server date (Given from kinvey request) into local timezone.
I'm using the following code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ss.sTZD"
print(dateFormatter.dateFromString(newValue))
The date format is this:
ect = "2016-08-28T16:30:06.553Z" or
lmt = "2016-08-28T16:30:06.553Z"
When I print the date it is nil, do you know what I'm doing wrong ?. I think it could be the end of the dateFormat
If your app can target only iOS7+, you can use format symbols described in:
Fixed Formats (in Data Formatting Guide)
Unicode Technical Standard #35 version tr35-31
second | S | 1..n | 3456 | Fractional Second - truncates (like other
time fields) to the count of letters. (example shows display using
pattern SSSS for seconds value 12.34567)
zone | X | 1 | -08,+0530,Z | The
ISO8601 basic format with hours field and optional minutes field. The
ISO8601 UTC indicator "Z" is used when local time offset is 0. (The
same as x, plus "Z".)
So, to parse fractional second, use uppercase 'S',
and 'X' for timezone including "Z" as UTC.
Try this:
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"
(I escaped 'T' as it may be used as another time formatting symbol in the future.)
PS. Though I couldn't have found a thread describing the date format which interprets "Z" as UTC+0000, ignoring or removing it may not be a bad solution, if some conditions met. Please find your best solution.
I'm trying to use LiveBindings to format a number for display in a TEdit on a FireMonkey form.
I'm trying to use the Format method in the CustomFormat of the binding to format the number with two decimal places.
I can 'hard code' the output:
Format("Hello", %s)
which is working, but I can't work out what formatting string to use. If I try a standard formatting string such as,
Format("%.2f", %s)
I get a runtime error "Format invalid or incompatible with argument".
Indeed I get an error whenever I include a % symbol in the format string, so I'm guessing Format takes a different type of argument, but I can't find any documentation to say what the correct format string is.
You can not use Format('%.2f',[%s]) in LiveBindings -> CustomFormat
The %s is reserved for the data and for a TEdit , it's a string
d : double;
s : string;
...
d := 1234.5678;
s:=Format('%.2f',[d]);
Format() is to convert [int, decimal, double, float] to a string .
all other give you a error : invalid argument
valid is for example
TLinkControlToField1 -> CustomFormat : "Double : "+UpperCase(%s)
will give you in Edit1.text
Double : 1234.5678
OK , we know that Uppercase() for '1234.5678' has no effects .
Is only to show (%s) is a string
Solutions:
Set to TFloatField -> DisplayFormat #00000.00
rounds and display 01234.57
check TFloatField -> currency
rounds and display 1234.57
use a component look here
LiveBindings in XE3: Formatting your Fields
The parameter is passed into CustomFormat as %s. The bindings system preparses out this parameter before the data is passed onto the evaluator. Thus any other % symbols in the CustomFormat string will give an error.
As with a normal format string you can include a literal % sign by putting a double % (i.e. %%).
So, any %s in the format string need to be converted to %%, e.g.
Format('%%.2f', %s)
which gets parsed out to
Format('%.2f', 67.66666)
and then parsed down to
67.67
for display.
If you want to include a literal % in the final output you need to put a quadrupal %, e.g.
Format('%%.2f%%%%', %s)
becomes
Format('%.2f%%', 67.6666)
and displays as
67.67%
Note: The normal format function takes a final parameter which is an array of values. The Format method in the bindings system takes a variable length list of parameters.
Also, the method names are case sensitive. 'Format' is correct, 'format' will fail.
imput 67.6666
CUSTOM FORMAT: ToStr(Format('%%.2f', Value)) + ' %%'
output 67.00 %