Validating date times (flutter/dart) - datetime

Has anyone gotten any meaningful use out of datetime.tryparse? I'm trying to accept date formats such as...
MM/dd/yyyy
MM-dd-yyyy
MM.dd.yyyy
it seems DateTime.tryParse always returns null for all of these formats. Is there a library or a more convenient way to accept date times of different formats.

DateTime.[try]parse only parses a very distinct format, namely:
a subset of ISO 8601 which includes the subset accepted by RFC 3339
To parse formats like 06/09/2019 use the DateFormat class from package:intl.
DateFormat.yMd().parse('06/09/2019'); // defaults to en_US, i.e. MM/dd/yyyy
This code:
import 'package:intl/intl.dart';
main() {
print(DateFormat.yMd().parse('06/09/2019'));
}
prints
2019-06-09 00:00:00.000
as expected

It is worse than you think:
DateTime.tryParse("2022-02-34")
DateTime (2022-03-06 00:00:00.000)
So for invalid input (format is correct but not the date) Dart knows better and retuns what it thinks is a valid date.
With invalid input there is no valid date

Related

How to change UK date format in LogicApp

Im trying to convert a U.K. input date (dd-MM-yyyy) to format (yyyy-MM-dd)
I tried
"#formatDateTime('15-03-2019','yyyy-MM-dd')" ==> Error
but got error:
'In function 'convertTimeZone', the value provided
for date time string '15-03-2019' was not valid. The datetime
string must match ISO 8601 format.'
How do I go about converting this input date? The input format is (dd-MM-yyyy) and cannot be changed.
I can easily convert from (MM-dd-yyyy) as shown below, but im not able to convert from (dd-MM-yyyy)
"#formatDateTime('03-15-2019','yyyy-MM-dd')" ==> OK
Date and time functions provided by azure logic app cannot recognize the timestamp in dd-MM-yyyy format.
After my research, there is no existing function that can directly solve this problem, but you can use substring and concat to deal with this problem.
The workflow of the logic app looks like this:
The expression of the formatDataTime:
formatDateTime(concat(substring(<your-date-string>,6,4),'-',substring(<your-date-string>,3,2),'-',substring(<your-date-string>,0,2)),'yyyy-MM-dd')

moment.utc - warning on invalid input string format

When testing my code that uses moment.utc I have some tests that use bad format: moment.utc('13-06-2018T13:00:00'). When doing this, I get this warning:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Why is that? I guess moment.utc is shorthand that is using UTC format under the hood, and I don't see a method version that accepts format so I cannot get rid of this warning.
How can I parse valid/invald utc string with moment.utc so I don't get this warnings?
As the warning and the linked guide, you have to take a look at moment(String) docs and use moment(String, String) for parsing strings not in ISO 8601 or RFC 2822 format.
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
Here a live example:
console.log( moment.utc('13-06-2018T13:00:00', 'DD-MM-YYYY[T]HH:mm:ss').format() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Can't format the date using moment.js

Can't format the below date using moment.js, the below statement returns Invalid Date
moment('20171206T062406927Z').format('D-MMM-YYYY');
Please help me on this.
You need to tell moment which format your date string is in:
moment('20171206T062406927Z', 'YYYYMMDD[T]HHmmssSSSZ', true).format('D-MMM-YYYY');
Edit: updated as per #VincenzoC comment to ensure the timestamp is parsed in UTC
Also fix: use HH for 24-hour format (not hh), and pass a third true parameter to ensure the timestamp is parsed in strict mode.

Moment JS Date format to remove minus sign from iso format

I am using moment.js in my application and the expected date format is
2017-01-09T17:05:00.000 //Expected Result
Where as if i call
moment().format()
I am getting ISO 8601 format i.e with T and minus sign
(2017-01-14T17:05:00-06:00) // Actual result.
What should i use to get this format of with .000
ISO 8601 is the default representation of moment's format() API. But you can customize it by passing required format pattern.
In your case, moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS') will produce what you're looking
2017-01-10T11:55:56.621
Find more customization options here: http://momentjs.com/docs/#/displaying/
--
Another option to know, though it works only for UTC time : moment().toISOString()
2017-01-10T06:56:18.465Z
[credit: Rajesh]

ColdFusion - DateTime Format with GMT offset

I am using ColdFusion 10 to make some REST calls and the date returned is using a GMT offset.
Example: 2013-03-25T14:30:40-04:00
I need this formatted for 2 purposes:
Screen Display so it looks something like mm/dd/yyyy hh:mm:ss
To Insert into mySQL.
I have tried a variety of the CF time/date functions but continue to get the "is not a valid date format"
I thought maybe the #ParseDateTime(i.submitted_at,"pop")# would handle it with POP but same issue.
Spent a few hours now trying multiple variations and googling around now just going in circles. Any ideas would be greatly appreciated.
Thanks!
Have a look at the UDF DateConvertISO8601() on CFLib.
DateConvertISO8601(ISO8601dateString, targetZoneOffset) on CFLib
Description:
This function take a string that holds a date in ISO 8601 and converts it to ODBC datetime, but could be adapted to convert to whatever you like. It also will convert to a datetime in a timezone of your choice by specifying the offset, i.e. it could take a datetime in GMT and convert to PT. See http://www.w3.org/TR/NOTE-datetime for description of ISO 8601, the International Standard for the representation of dates and times.
Return Values:
Returns a datetime.
The source code is viewable at the link I provided.
This, 2013-03-25T14:30:40-04:00, is a string. If you run this:
x = left(2013-03-25T14:30:40-04:00, 19);
you get 2013-03-25T14:30:40. You can use the replace function to replace the T with a space. You can then to this
DateTimeVar =parsedatetime('2013-03-25 14:30:40');
Now you have a datetime variable that you can format. If necessary you can do a datediff with the offset from GMT.
This is an informational answer, not a direct answer to the question.
ColdFusion 11 has updated the ParseDateTime() function so that it will correctly convert the ISO-8601 date/time strings to a ColdFusion datetime object.
With a number of remote requests and responses, the date / time values can often be returned in ISO format. In your case, the mask looks like this:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
In this ISO format, the T string is a literal representation of a marker where the time stamp starts in the string (with the offset following directly).
Below is a reusable function that will convert an ISO date format into a useable ColdFusion date time object:
<cffunction name="ISOToDateTime" access="public" returntype="string" output="false"
hint="Converts an ISO 8601 date/time stamp with optional dashes to a ColdFusion
date/time stamp.">
<cfargument name="Date" type="string" required="true" hint="ISO 8601 date/time stamp." />
<cfreturn ARGUMENTS.Date.ReplaceFirst(
"^.*?(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*$",
"$1-$2-$3 $4"
) />
</cffunction>
You can then call the function like so to output or return a ColdFusion-friendly version of the date time:
ISOToDateTime( "2013-03-25T14:30:40-04:00" )
That function is courtesy of Ben Nadel. The original blog post can be found here:
http://www.bennadel.com/blog/811-Converting-ISO-Date-Time-To-ColdFusion-Date-Time.htm
You can also convert the date time value using the offset, if required. Again, Ben Nadel has a great blog post outlining how to accomplish this:
http://www.bennadel.com/blog/1595-Converting-To-GMT-And-From-GMT-In-ColdFusion-For-Use-With-HTTP-Time-Stamps.htm
CF10 can use this code as stated in the example of the parseDateTime() doc.
<cfset string = "1997-07-16T19:20:30+01:00">
<cfset date = parseDateTime(string, "yyyy-MM-dd'T'HH:mm:ssX")>

Resources