Unable to parse yyyyMMdd with DateFormat of intl in dart - datetime

I have a date string like '20200814' that means day 14 of month 08 of year 2020.
In the docs of intl it said this:
'yyyy.MM.dd G 'at' HH:mm:ss vvvv' 1996.07.10 AD at 15:08:56 Pacific Time
So if I use this:
DateFormat('yyyyMMdd').parse('20200814')
It must works, but throw this error:
════════ Exception caught by animation library ═════════════════════════════════
The following FormatException was thrown while notifying status listeners for AnimationController:
Trying to read MM from 20200814 at position 8
When the exception was thrown, this was the stack
#0 _DateFormatField.throwFormatException
package:intl/…/intl/date_format_field.dart:87
#1 _DateFormatPatternField.parseField
package:intl/…/intl/date_format_field.dart:337
#2 _DateFormatPatternField.parse
package:intl/…/intl/date_format_field.dart:250```

I've taken a look at the intl source, and unfortunately the library does not support parsing a date string containing numerical components that are not separated by non-numeric characters.
The reason for this is that parse(input) converts the input string to a stream, and when it tries to extract the year value, it calls the _Stream instance method _Stream.nextInteger() on the input stream, which then consumes the entire string, since the whole string can be read as a single integer. This leaves nothing in the stream to be parsed as month or day, which is why the error is thrown.

As #Michael Horn mentioned, The package:intl's DateFormat library does not support parsing date strings unless they are character-delimited.
In this case, parser sees all the numbers entered as years, so it occurs an error trying to find the month according to the pattern.
So we have to put some separator between the input.
String input = '20210701';
/// convert 20210701 to 2021/07/01
String newInput = '${input.substring(0, 4)}/${input.substring(4, 6)}/${input.substring(6, 8)}';
DateTime date = DateFormat('yyyy/MM/dd').parse(newInput);

Related

Add a day to a datetime project parameter and store in a variable of type datetime in using SSIS expression

I am trying to add a day to a project parameter of type DATETIME and store the results in a variable in SSIS which of type DATETIME.
I am using this below expression in variable
dateadd(day,1,(DT_DATE)(DT_DBDATE) #[$Project::Start_Date])
and getting this below error
TITLE: Expression Builder
Expression cannot be evaluated.
For help, click:
http://go.microsoft.com/fwlink?ProdName=Microsoft%C2%AE%20Visual%20Studio%C2%AE%202015&ProdVer=14.0.23107.0&EvtSrc=Microsoft.DataTransformationServices.Controls.TaskUIFramework.TaskUIFrameworkSR&EvtID=FailToEvaluateExpression&LinkId=20476
------------------------------ ADDITIONAL INFORMATION:
The expression contains unrecognized token "day". If "day" is a
variable, it should be expressed as "#day". The specified token is not
valid. If the token is intended to be a variable name, it should be
prefixed with the # symbol.
Attempt to parse the expression "dateadd(day,1,(DT_DATE)(DT_DBDATE)
#[$Project::Start_Date])" failed and returned error code 0xC00470A4.
The expression cannot be parsed. It might contain invalid elements or
it might not be well-formed. There may also be an out-of-memory error.
(Microsoft.DataTransformationServices.Controls)
------------------------------ BUTTONS:
OK
Can anyone help me to resolve the above problem.
TL;DR;
The argument to the first parameter for dateadd is a string, not a constant/enumeration so it should be
dateadd("day",1,(DT_DATE)(DT_DBDATE) #[$Project::Start_Date])
The long way around
I assume the desire is to get the next day's date with the supplied expression
dateadd(day,1,(DT_DATE)(DT_DBDATE) #[$Project::Start_Date])
When I run into issues with expressions, I break them down into the most atomic statement and then compose from there.
I'm using a SSIS scoped variable instead of a project parameter but the logic will hold true.
I have an SSIS variable, Start_Date of data type DateTime with an initial value of 2022-06-01 09:22 AM (convert that to your current locale's preference for date presenation)
I created a new variable, Start_DateOnly and used the following expression
(DT_DATE)(DT_DBDATE) #[User::Start_Date]
Great, that shows 2022-06-01 (no time component in the Variables window although if you evaluate in the Expression editor, it will show midnight). And the explainer - we convert to the DT_DBDATE datatype to drop the time component but DT_DBDATE is incompatible with the displayed DateTime data type so we explicitly convert to DT_DATE.
Cool beans, now all we need to do is confirm the dateadd function works as expected with our new variable
dateadd(day, 1, #[User::Start_DateOnly])
What the heck?
Expression cannot be evaluated.
The expression contains unrecognized token "day". If "day" is a variable, it should be expressed as "#day". The specified token is not valid. If the token is intended to be a variable name, it should be prefixed with the # symbol.
Oh... yeah, while this language is similar to TSQL, the datepart parameter is a string, not an enum/constant so the syntax should be
dateadd("day", 1, #[User::Start_DateOnly])
Yup, that evaluates to 2022-06-02 12:00 AM

FTL Date Formatting In Netsuite

I have a date coming through as 25/4/2022. I need it changed to 25/APRIL/2022. I have tried every combination of ?date('dd/mm/yyyy') / ?datetime('dd/mmm/yyyy') ?datetime(dd/mm/yyyy)?string('dd/mmm/yyyy') that I can think of but I keep getting teh same type of errors:
The string doesn't match the expected date/time/date-time format.
The nested reason given follows: Unparseable date: "" ---- FTL stack trace ("~" means nesting-related)
What am I missing here?
You need to use a Java SimpleDateFormat pattern to format dates. If you want the month, use M (uppercase), as m (lowercase) is minutes in hour. For a full month, use MMMM. So, use:
${"25/April/2022"?date("dd/MMMM/yyyy")}
See also:
https://freemarker.apache.org/docs/ref_builtins_string.html#ref_builtin_string_date
https://freemarker.apache.org/docs/ref_directive_setting.html#topic.dateTimeFormatSettings
How to parse month full form string using DateFormat in Java?

DateTime format for 9/14/2016 19:31

I am doing an F# course by Richard Broida. In one lecture I am supposed to parse a CSV document. Everything works fine except for parsing datetime. My local culture settings are Slovak (sk-SK) and I am trying to parse the following datetime string:
9/14/2016 19:31
I have read through the CSV file and found out that months, days and hours can all have both one or two digits. Years always have 4 digits and minutes always have 2 digits. So, the datetime 10/7/2016 9:01 is also a valid one.
I have created an adapter function:
let dateTimeParseAdapter format provider date =
DateTime.ParseExact(date, format, provider)
and then called
dateTimeParseAdapter "M/d/yyyy h:mm" CultureInfo.InvariantCulture "9/14/2016 19:31"
but I got an exception message saying
System.FormatException: String was not recognized as a valid DateTime.
I tried adjusting the format string to exactly match the number of digits in my single date. However, even the following code resulted in the same error message.
dateTimeParseAdapter "M/dd/yyyy hh:mm" CultureInfo.InvariantCulture "9/14/2016 19:31"
Why am I getting the exception message? Should I change the CultureInfo settings? Or is there a pre-defined datetime format to parse my datetimes?
When working with 24 hour format you should use H (HH) not h hh (which is for 12 hour format) pattern:
format = #"M/d/yyyy H:mm";
since you are using 24 hour in your value you must put capitals in hours. Change
"M/dd/yyyy hh:mm"
to
"M/dd/yyyy HH:mm"

moment.js and deprecated warning. timestamp to moment date object

I've read thru various posts on here in regards to similiar issues but none have solved my problem.
I manipulate the moment.js date object, and then store it as timestamp.
BUT, when I try to read in that timestamp again, I get that deprecated warning.
""Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info."
I've tried toDate(), format(), moment(myTimeStamp, 'ddd, DD MMM YYYY HH:mm:ss ZZ'); --> all generate the warning...
So, for example, my timestamp will look like this:
const timestamp = '1458586740000'
when I read that back and try to parse out the month/day/year, then the hour/min am/pm, etc... I need to get that timestamp into a moment.js object. Nothing is working for me. Any ideas.
How can I get this timestamp: '1458586740000', into a moment.js object so I can extract date date from it as I need?
EDIT: this is how I am storing the timestamp. So I would need to retrieve it from this.
let timeStamp = Moment(state[_Date])
.add({ hour: state[AMPM] === 'PM'
? +state[Hour] + 12
: state[Hour] ,
minute: state[Min] }).format('x')
The X token indicates a unix timestamp in seconds, and the x token indicates a unix millisecond timestamp (offset).
You appear to have a millisecond timestamp, so you would make a moment out of it by doing the following:
var a = moment('1458586740000', 'x')
It works without ' as well:
var a = moment(1458586740000, 'x')
You can also not specify the x and it should work:
moment(1458586740000)
Because you have a unix offset (milliseconds), not a unix timestamp (seconds), moment.unix is not what you want.
Then you can do the following:
a.format()
"2016-03-21T13:59:00-05:00"
Or you can use any of the other formatting tokens listed here to output whatever result you would like: http://momentjs.com/docs/#/displaying/format/
Based on the code you presented, I think you may be having problems because your timestamp is stored as a string (in ''). Parsing as a string causes an invalid date error, because it attempts to match ISO 8601 format and fails. Specifying that 'x' token will cause it to assume unix offset and work correctly.

String conversion to DateTime not working

I'm trying to save data of birth in database from client but it gives format exception
heres my conversion
cmd.Parameters.AddWithValue("#dob", DateTime.ParseExact(dob.Text ,"dd-mm-yyyy",System.Globalization.CultureInfo.InvariantCulture));
input example dob.text="22-2-2012"
in this case exception is "String was not recognized as valid dateTime"
but in case of "22-12-2012"
the exception is "Conversion failed when converting date and/or time from character string"
Try changing it to
DateTime.ParseExact(dob.Text, "dd-M-yyyy", System.Globalization.CultureInfo.InvariantCulture)
instead (note the single capital M instead of your double small m)
Have a look at Custom Date and Time Format Strings and look at the difference between a single M and a double M.

Resources