How to change Time Format in VB.NET from 24 to 12? - vb.net

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")

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

How do I get the right date format after using get text on the folder date?

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")

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);

DateTime.Date (long value)

I have spent quite a few hours and still unable to understand this:
Dim unix_time_at_midnight As Long
DateTime.DateFormat = "MM/dd/yyyy"
unix_time_at_midnight = DateTime.DateParse(DateTime.Date(unix_time*1000))/1000
where both unix_time_at_midnight and unix_time are long values. I understand DateTime.DateParse excepts a String and converts it to DateTime. What is (DateTime.Date(unix_time*1000))/1000 returning and what is its equivalent in Java? The requirement is to get the number of seconds since GMT midnight and I have successfully implemented it in Java. However, I would like to understand this particular line of code written in VB.net
EDIT: This method was written in Basic4Android and probably constitutes more of its libraries then vb.net. However, I have looked into each for details but unable to understand. Would appreciate if you could elaborate. Please see the links.
Take this:
DateTime.Date(unix_time*1000)
The documentation says:
Date (Ticks As Long) As String
Returns a string representation of the date (which is stored as ticks).
The date format can be set with the DateFormat keyword.
So that part returns a string representing the date.
It then uses DateTime.DateParse, which is documented as:
DateParse (Date As String) As Long
Parses the given date string and returns its ticks representation.
Taken together, this appears to take the ticks, multiplied by 1000, converted to a string that doesn't contain hour information which is parsed back to ticks which are divided by 1000.
The important thing to note is that the DateFormat set on the line before contains only the formatting for the date, no hours/minutes/seconds and smaller units of time exist in it. This means that the string returned essentially represents midnight of that date.

Visual basic script that control a date format

I would like to control a string if is in the dd/mm/yyyy format and if the dd number is between 1 and 31 and if mm is between 1 and 12.
In vb.net you can use the IsDate() function to test the validity of a date. This will insure that the day and the month are within the valid range.
You can use the DatePart function:
DatePart("m", date)
DatePart("d", date)
Wait what? Your question is not very clear. Do you have a DateTime and need to output it in a specific format? Are you accepting a string from the user and need to make sure it fits that format? Do you get a string from somewhere else that you need to match for a specific format?
Most of all, why do you care? You shouldn't be dealing with dates as strings, except at the point of interaction with the user or other data source. Inside your program they should be a DateTime type. Assuming you're 'vb.net' tag is correct, the DateTime has handy Parse, TryParse, and ParseExact, and TryParseExact static methods you can use to accept most anything the user could throw at you.