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"
Related
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')
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);
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.
I am using entity framework in an asp.net web forms application, and binding my model using code nuggets / directives. I have a DateTime field -ClosedDate- and I am trying to display the hours between that and the current date and time, which represents the remaining hours. The value it produces without formatting it is correct, I just want to change the format.
For 2 hours and 30 minutes remaining, it is displayed as 2.50000000000000, and I would like it displayed as either 2 or 2.5. I tried toString() with custom format strings, but could not get the correct value
The Code
<%# Item.ClosedDate.Subtract(DateTime.Now).TotalHours%>
This produces 2.50000000000000
<%# Item.ClosedDate.Subtract(DateTime.Now).TotalHours.ToString("hh")%>
This produces hh
<%# Item.ClosedDate.Subtract(DateTime.Now).ToString("hh")%>
This produces 02, which is fine, but when ClosedDate is greater than 24 hours, the value is not correct.
When ClosedDate = 2013-12-10 01:10:25.000 and DateTime.Now = 2013-12-07 20:20:00.000, the value produced was 04.
When ClosedDate = 2013-12-10 19:54:11.000 and DateTime.Now = 2013-12-07 20:20:00.000, the value produced was 23.
A couple of things here. This:
Item.ClosedDate.Subtract(DateTime.Now).TotalHours
returns a double, and this:
Item.ClosedDate.Subtract(DateTime.Now)
returns a TimeSpan, not a DateTime.
So, you've been trying to format a double or a TimeSpan as an hour "hh", but the time and date format specifiers should only be used with DateTime structs. If you just want the difference in hours with a specific number of decimal places, use a built-in or custom number format that will pad your result, such as:
Item.ClosedDate.Subtract(DateTime.Now).TotalHours.ToString("#0.##");
The above should display 2.5 as 2.5, and 2.0 as 2.
See the Standard Numeric Format Strings and Custom Numeric Format Strings documentation on MSDN for more examples.
I am trying to convert DateTime object to string using formatting but due to some strange reason, it ignores AM/PM. It would just take the magnitude of the Hour in the object.
I am using the following format:
StartDate.ToString("yyyy-MM-dd hh:mm:ss.fff");
and
String.Format("0:yyyy-MM-dd hh:mm:ss.fff", StartDate);
I don't think that there is a difference between the two, but just wanted to give it a try. If I pass a value 4/28/2012 6:00:00 AM or 4/28/2012 6:00:00 PM, the result is the same "2012-04-27 06:00:00.000"
You've used hh which uses the 12-hour clock. You want HH which uses the 24-hour clock.
See MSDN for more details about custom format strings.
Note that you may wish to specify the invariant culture, unless you really want the time separator to depend on the current culture:
string formatted = StartDate.ToString("yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture);
(Note that you shouldn't have the braces if you're passing the format string to ToString. I'll assume this was just a typo in the question.)
If you want to use the 12-hour clock, use tt in the format string to produce the AM/PM designator.