Date format issue - vb.net

I'm facing problem while conversion of date
if I used -- Date.parse("28/01/2011")
it gives me error as
"String was not recognized as a valid DateTime."
so then I modify above code as -- CDate("28/01/2011")
it gives me error as
"Cast from string "28/01/2011" to type 'Date' is not valid."
I used convert.todatetime also date.parseexact but nothing is working...
I'm using VS2003 in asp.net1.1 with vb.net

Probably Parse is using InvariantCulture date format "MM/dd/yyyy". Maybe you can try with
DateTime.ParseExact("28/01/2011", "dd/MM/yyyy", CultureInfo.InvariantCulture)
or passing a correct culture on Parse, like spanish that has date format dd/MM/yyyy
Date.Parse("28/01/2011", new CultureInfo("es-ES", true));
Write from memory, maybe is not accurate

Related

Date.ParseExact issues

I'm trying to format a date for an API. the desired format is: yyyy-MM-ddTHH:mm:ss.fffffff+HH:mm
(eg. 2022-10-12T09:52:14.1234567+03:00). I'm using Date.ParseExact in the following way:
Date.ParseExact("2022-10-12T09:52:14.1234567+03:00", "yyyy-MM-ddTHH:mm:ss.fffffff+HH:mm", CultureInfo.InvariantCulture)
.
Initially I used 'Now' instead of this string, but then I saw that the string and the desired format have to match. The error I'm getting is 'DateTime pattern 'H' appears more than once with different values.'. Is there a way to avoid that? Also is it possible to use 'Now' in this line?
Thank you
I suspect that you don't have a parse issue, you don't need ParseExact at all. You have a Date and want to return it as a formatted string. Then use ToString and zzz for the utc-offset:
string result = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz");
Read also: Custom date and time format strings

Firebird ISC 335544334 - Conversion error from string "2002-07-07 22:00:00.000"

I am currently working on a project which allows you to import data from an Excel file. I use the Programm RazorSQL. But every time I start the import, the
ISC error code 335544334 occures ->
An error occurred: java.sql.SQLException: conversion error from
string "2002-07-07 22:00:00.000" [SQLState:22018, ISC error
code:335544334]
Does Firebird use some special Date formats? I really need an answer, it's very important. This problem keeps me waiting to continue with my work...
The error message indicates that Firebird tried to convert the string value 2002-07-07 22:00:00.000 to a datatype other than CHAR/VARCHAR and did not succeed because the string value is invalid for the target datatype. The format as shown will correctly convert to a TIMESTAMP, which means that you are assigning this either to the wrong column, or the column has the wrong type. For example, a DATE has no time component in dialect 3, so converting a string with a time component will fail with this error, as will converting to a numerical column (INTEGER, BIGINT, etc).
Without more information about the columns involved and the exact method of import, it is not possible to provide a more specific answer.

Converting one unknown DatePattern to a known pattern

I get the user machine's date pattern using this:
Dim sysFormat As String = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
so for example it comes as M/d/yy but in my program I want to parse them in a M/d/yyyy format. But they could even have some other format, we don't know what format. It is ALWAYS gonna be US English tho.
So is there a way to automatically convert whatever it is to the M/d/yyyy format ? or do I have to manually do some string processing code and split the string to different parts for day,month, year?
Have you tried formatting your String to a Datetime object and then parsing using a specific formatter?
Dim myDate As DateTime = DateTime.ParseExact(sysFormat, "M/d/yyyy",
System.Globalization.CultureInfo.InvariantCulture)
And your new String (the one that's formatted) is:
Dim formattedStringDate As String = myDate.ToString("M/d/yyyy")
The requirements you have seem quite specific. You could use DateTime.TryParseExact to try parsing a few valid formats and check whether any results in a valid date. With new C# features you could even get rid of the extra out-parameter declaration. For example:
DateTime.TryParseExact(dateTime,
"M/d/yy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
DateTime out dt);

vb.net datetime.parseExact error message saying invalid format

Hi am trying to use the datetime.parse exact object in vb.net, but I keep getting an error saying invalid format. Here is my statement, could anyone tell me what I am doing wrong??
Dim TimeStart As Date
TimeStart = DateTime.ParseExact("2013.07.15-07:10:02", "yyyy.MM.dd-HH:MM:SS", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
The error is because the seconds must be lower case. Also, you're asking for the month twice. I suspect this is the format string you need:
yyyy.MM.dd-HH:mm:ss
Just as an informational bit, the upper case 'H' means it's looking for 24-hour time, instead of 12-hour time. Lower-case 'h' would mean 12-hour time. Here is the reference for custom datetime format string rules:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Date.parse function error

i am using the date.parse function to format the date input in the textbox.
However if I input a future date the date parse function fails.
Why is it so?
I need to format the date and also input future dates.
If IsDate(TransactionDate.Text) Then
TransactionDate.Text = Date.Parse(TransactionDate.Text)
Else
MsgBox("Enter correct Transaction date")
TransactionDate.Focus()
End If
i used 12/5/2013 and it worked fine.
I suspect it didn't, actually. I suspect it actually parsed it as December 5th 2013, when you meant May 12th 2013.
It seems that it's trying to parse it as a format of MM/dd/yyyy, whereas you mean dd/MM/yyyy.
That could be because the thread you're using is in a US culture, for example.
I suggest that you use DateTime.TryParseExact:
If you want to specify a precise format, you can do that using a custom date/time format pattern
If you want to allow .NET to use an appropriate format for the culture, you can do that using a standard date/time format pattern
Specify the culture you want to use for parsing, using CultureInfo.InvariantCulture if it's meant to parse machine-generated text. You can use null to mean "the culture of the currently executing thread" but personally I'd make that explicit, for readability
Use the return value to determine whether parsing failed. If this indicates a bug somewhere, then it would make sense to use DateTime.ParseExact instead, as that will throw an exception. Basically you need to work out what you want to do on error.
You need to specify the format you are expecting the data string to be in. Dates can be written in many formats - US (mm/dd/yyyy) or European (dd/mm/yyyy), two digit years (yy) or four digit years (yyyy), different separator (/) vs (-) and so on. The list is endless.
You either need to state explicitly what date you expect or write your code to try various formats and cope with incorrect inputs.
The danger with the latter approach is that dates can be ambiguous - is "1/2/2013" the 1st of February or the 2nd of January?
Use an overload of Date.TryParse that takes an IFormatProvider and report an error if it fails.
dateString = "2008-03-01 10:00"
culture = CultureInfo.CreateSpecificCulture("fr-FR")
styles = DateTimeStyles.AdjustToUniversal Or DateTimeStyles.AssumeLocal
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
Use DateTime.ParseExact and pass the format and provide.
dateString = "13/05/2013";
format = "dd-MM-yyyy";
CultureInfo provider = CultureInfo.InvariantCulture;
var result = DateTime.ParseExact(dateString, format, provider);