I recently asked this question:
DateTime.ParseExact was not recognized as a valid DateTime
The question was answered and the approach worked great. This was until I tried running it on a windows 10 machine. I then get the error:
String Not Recognized As Valid DateTime
By "String" I assume the debugger is referencing the dd/MM/yy. Does anyone have any idea why this may be? Here's the code if you don't want to trawl through the linked question:
Tabledate = DateTime.ParseExact("30/11/12", "dd/MM/yy", CultureInfo.InvariantCulture)
UPDATE 1
Thanks to #Blackwood for the comment about the actual computers time, I just changed window's 10 Short Date format to "dd/MM/yy" from "dd/MM/yyyy" and the code runs fine!
Now I don't know what format other people's computers may be in, so I suppose my next approach will be to research if I can obtain what format the current PC is in and then use that format as a variable
I guess this has something to do with Culture, ist seems to be the dateformat u r passing : "dd/MM/yy"
Try this :
DateTime.ParseExact("30/11/12","d", CultureInfo.InvariantCulture)
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
Related
I have tried everything that I can think of to get the right date format. Can anybody help with this RPA-problem in UiPath. I have used the 'get text' activity to get the folder date and after that tried to use the
Datetime.ParseExact(Str variable,"dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture).
It gives me the error:
Assign: String was not recognized as a valid DateTime.
Your help is much appreciated.
edit: I have now sought help from a friend who figured it out. The error was in the string, which could not be seen, because there was an invisible character in the string that I extracted through 'get text' activity. The solution is in 2 steps:
assign another variable to the exact same date and use an if statement to find out if these two strings are equal and you will find out that they are not.
Now use a regex to capture only digits and slash/hyphen which will get rid of the invisible character.
Try to use "dd_MM_yyyy" instead of "dd/MM/yyyy".
The reason is because UiPath/VB.Net has a habit of using US date formatting even though you're using Culture Info...It's a real pain
Try this:
pDateString = "21/02/2020"
Assign a Date type variable = Date.ParseExact(pDateString,"dd/MM/yyyy",nothing)
Here we're telling the parser to use English format date...The Date type returned will be in US format but you can simply convert back to uk IF needed by using something like:
pDateString("dd/MM/yyyy")
I regret having to write such a vague question, but I'm not really sure what to try. I have general programming experience, but no access to the source code for this and not a lot of experience with VB/.NET
We have a custom (Read: Crappy) CRM software we use at work that was written ~2008 by someone who has long since left the company. It works on both Windows 7 and Windows 10 just fine for multiple users. I am trying help a user with an "Unexpected program error":
"frmMain - FillGridTable
Conversion from string "03/31/2016 15:23:22" to type 'Date' is not valid."
when I close the dialog box it just says "LoopX = 0"
I do not have access to the source code, and regardless, it works on every other computer.
Could this be some issue with some type of library on the computer? Is there any way to check versions between computers for .NET/Microsoft libraries?
Thanks for any ideas!
Chase Rocker helped me get here in the comment he left on the question.
The windows date/time format settings were displaying a date that wasn't in the correct format. Resolved this by changing it to the MM/dd/YYYY format the error was suggesting the program expected.
I'm glad to see that you solved the issue by changing the computer's regional settings, but to be honest this is probably overkill for what you're wanting to do. Instead, you could have used the DateTime.TryParseExact function.
Here is a quick example of utilizing the built-in method:
Dim conversion As DateTime
Dim input As String = "11/30/2017"
If DateTime.TryParseExact(input, "MM/dd/yyyy", New CultureInfo("en-US"), DateTimeStyles.None, conversion) Then
'Converted successfully
Else
'A true conversion error occured
End If
Fiddle: Live Demo
I’m having a problem with date formats.
I need to convert the following string into af date object: 2011-09-19T12:23:51Z
And then convert the date object back to a string with this format: 19. september 2011
I can’t figure out what the “T” and “Z” is all about though?
Can anyone help me?
Kind regards
Jesper
The "T" is to separate the date from the time.
The "Z" shows that this is in UTC.
This is a standard (extended) ISO-8601 format date/time string - it should be easy to parse with whatever libraries iOS provides.
I am using these codes for displaying time in VB.NET
it shows up in 24 hours format besides i need it in 12 hours format
System.DateTime.Now.Hour
System.DateTime.Now.Minute
System.DateTime.Now.Second
example:
14:12:42
I need it as :
02:12:42
thanks.
Use String.Format. For example:
String.Format("{0:T}", System.DateTime.Now) //02:12:42 PM
String.Format("{0:hh:mm:ss}", System.DateTime.Now) //02:12:42
String.Format("{0:hh:mm:ss tt}", System.DateTime.Now) //02:12:42 PM
Also, this website to be very helpful in summarizing the various ways you can use String.Format. Keep in mind the culture can make a difference on non-custom formats. The first example above using T (Long Time format) works on my US-based PC just fine. But if you say:
String.Format(System.Globalization.CultureInfo.InvariantCulture, _
"{0:T}", System.DateTime.Now)
You end up with 14:12:42. The latter two examples are custom formats and are not affected by culture.
When using DateTime objects you can actually use the ToString() method and set your format inside it.
string currentTime = System.DateTime.Now.ToString("hh:mm:ss");
Check this msdn article out for more clarity:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
Use the appropriate format string for display.
string formatted = myDateTime.ToString("hh:mm:ss");
I have used a custom format string in this case.
1-Use regex to get first two characters of that string ie from 23:11:59 get 23
2-convert this number to integer type
3-now check it if it is not greater than 12 and if it is subtract 12 from it and by using string.replace replace the old value.
Try This...
Dim CurTime As String
CurTime = TimeOfDay.ToString("h:mm:ss tt")
I'm working in a web application using VB.NET. There is also VisualBasic code mixed in it, in particular the Date variable and the Month function of VB.
The problem is this part:
Month("10/01/2008")
On the servers, I get 10 (October) as the month (which is supposed to be correct). On my machine, I get 1 (January) (which is supposed to be wrong).
Two of my colleagues (on their own machines) get different answers, one got 1, the other got 10.
The question is, why is this so?
On my end, I can solve the problem by using .NET's DateTime's Parse (or ParseExact) function to force everything to be "dd/MM/yyyy" format. This works. I'm just wondering why there's an inconsistency.
Extra info: I know the parameter for Month function is supposed to be a Date variable. The code used a string as parameter, and Option Strict was off, and the developers mainly let VB do its own conversion thing. (Legacy code maintenance has a lot of inertia...)
If it helps, the version of Microsoft.VisualBasic.dll on the servers is 7.10.6310.4 (under the Framework folder v1.1.4322). The version on mine (and my 2 colleagues') machine is 7.10.6001.4.
Edit: Regional settings for all machines already set to dd/MM/yyyy format (short date format).
This normally has to do with the regional settings, and more specifically the date/time formats. If you set these formats so that they are all the same on the machines you're testing on, the results should be consistent.
Your idea of using ParseExact is definitely the better solution to go with, IMHO.
This is because the runtime has to convert your given value "10/01/2008" which is indeed a string implicitly to the DateTime datatype.
When converting strings to dates and the other way round, the string format depends on the locale settings of windows.
See this link on msdn.
In this article a way to specify a date literal which is independent of your locale settings:
Just enclose the date with the sign # and specify it in the form mm/dd/yyyy:
So the code
Month(#10/01/2008#)
should give you the answer 10 on any machine.
Ther a two more worarounds given in that msdn article:
1. Use the Format Function with predifned Date/Time Format
To convert a Date literal to the
format of your locale, or to a custom
format, supply the literal to the
Format Function, specifying either
Predefined Date/Time Formats (Format
Function) or User-Defined Date/Time
Formats (Format Function). The
following example demonstrates this.
MsgBox("The formatted date is " &
Format(#5/31/1993#, "dddd, d MMM
yyyy"))
2. Use the DateTime-Class Constructor to construt the right DateTime value
Alternatively, you can use one of the
overloaded constructors of the
DateTime structure to assemble a date
and time value. The following example
creates a value to represent May 31,
1993 at 12:14 in the afternoon.
Dim dateInMay As New
System.DateTime(1993, 5, 31, 12, 14,
0)