Date.Now not displaying 12 hour format - vb.net

I know this probably is considered a repeat question so I will apologize in advance, but I have looked at question after question and everyone says the "hh" will display a 12 hour format. I keep getting a 24 hour format as if I have "HH". What gives?
strTime = Date.Now.ToString("hhmmsstt")

HH is 24 hour format while hh is 12 hour one

https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
On this link you can check all the possible time formats, so you are interested about the following:
"h"
The hour, using a 12-hour clock from 1 to 12.
More information: The "h" Custom Format Specifier.
"hh"
The hour, using a 12-hour clock from 01 to 12.
More information: The "hh" Custom Format Specifier.
"H"
The hour, using a 24-hour clock from 0 to 23.
More information: The "H" Custom Format Specifier.
"HH"
The hour, using a 24-hour clock from 00 to 23.
More information: The "HH" Custom Format Specifier.

You can use it like this
strtime = Format(Now, "hh:mm tt")
You get an output like this
04:33 PM
If for something you dont want the 0 behind 4 you just take out one h
strtime = Format(Now, "h:mm tt")

You can try
strTime = Date.Now.ToLocalTime()
to get a local time.

Related

how can I cover [MM DD YYYY hh:mm:ss:mmm(AM/PM)] to unix timestamp?

I want to convert the following date format to "May 13 2020 12:00:00:000AM" to UNIX formatted timestamps in milliseconds. Can you please advice on doing this. Thanks in advance.

VB.NET compare date time from string format

I have this string: 28 June 2018 (22:05)
How can I compare it with my current time and get the difference?
For example if actual time was 29/06/2018 (05:49)
The difference will be: 7 hours 44 minutes
So input: 28 June 2018 (22:05)
Output: 7 hours 44 minutes
The first thing you need to do, is convert the string to a valid DateTime instance.
If you know your dates will always be in this format, you can do the following...
Dim mydate = DateTime.ParseExact("28 June 2018 (22:05)", "dd MMMM yyyy (HH:mm)", CultureInfo.InvariantCulture)
https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
Once you've parsed the string into a valid DateTime instance, you can use all the normal date functions to do the comparisons.
I would first get the difference in minutes, like so...
Dim diffminutes = DateDiff(DateInterval.Minute, mydate, Now)
Then create a timespan like this...
Dim mytimespan = TimeSpan.FromMinutes(diffminutes)
Finally display the difference in hours and minutes like this...
Response.Write(mytimespan.ToString("hh\:mm"))

SQL Automatically converts 24 hour format time to 12 hour format

Time is visible on my table as follows
2017-09-15 16:30:00.000
And my Query is like this
Format(SlotStartTime,'dd/MM/yyyy hh:mi:ss') as SlotStartTime
And this returns time like this
15/09/2017 04:30:00
So the calculation gets wrong it should be 15/09/2017 04:30:00 PM or stay as it is in 24-hour format. How can I achieve this?
hh gives you 12 hour hours
HH gives you 24 hour hours
Also, mi is probably a typo, as it will return you a minute, followed by the letter i. Try this:
declare #datetime datetime = '2017-08-29 16:30:01'
select Format(#datetime,'dd/MM/yyyy HH:mm:ss') as SlotStartTime
You can find all valid datetime formatting characters here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
hh is the code for 12 hour format. HH is the code for 24 hour format. It's similar to how mm (not mi) is for minutes and MM is for months.
You want:
FORMAT(SlotStartTime,'dd/MM/yyyy HH:mm:ss')
12 hour with the time period:
FORMAT(SlotStartTime,'dd/MM/yyyy hh:mm:ss tt')

Kendo UI DateTimePicker Getting Wrong Time Format ASP.NET C#

I have some records in my database as below:
Note that the StartDateTime and EndDateTime are in 24 hours-system.
But when I display the datetime in my view, the time format is 12 hours-system as below:
From the html code, the value for "1st" End Date Time is correct, but in 12 hours-system:
The dates are used to calculate the amounts. This resulted in wrong number of hours calculated based on the kendoui datetimepicker. Can i know how to solve this?
Here is the JS:
$("#Payment_End_Date_" + count).kendoDateTimePicker({
format: "dd/MM/yyyy HH:mm",
timeFormat: "HH:mm",
interval: 60
});
Change format to
format: "dd/MM/yyyy H:mm",

Rails daylight savings time not accounted for when using DateTime.strptime

I've been working on parsing strings and I have a test case that has been causing problems for me. When parsing a date/time string with strptime, Daylight Savings Time is NOT accounted for. This is a bug as far as I can tell. I can't find any docs on this bug. Here is a test case in the Rails console. This is ruby 1.9.3-p215 and Rails 3.2.2.
1.9.3-p125 :049 > dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z")
=> Sun, 15 Apr 2012 10:00:00 -0600
1.9.3-p125 :050 > dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z").utc
=> Sun, 15 Apr 2012 16:00:00 +0000
1.9.3-p125 :051 > dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z").utc.in_time_zone("Central Time (US & Canada)")
=> Sun, 15 Apr 2012 11:00:00 CDT -05:00
As you can see, I have to convert to utc and then back to the timezone to get DST to be properly interpreted, but then the time is shifted one hour as well, so it's not what I parsed out of the string. Does someone have a workaround to this bug or a more robust way of parsing a date + time + timezone reliably into a DateTime object where daylight savings time is properly represented? Thank you.
Edit:
Ok, I found a workaround, although I'm not sure how robust it is.
Here is an example:
ActiveSupport::TimeZone["Central Time (US & Canada)"].parse "2012-04-15 10:00"
This parses the date/time string into the correct timezone. I'm not sure how robust the parse method is for handling this so I'd like to see if there is a better workaround, but this is my method so far.
This is a frustrating problem. The Rails method you're looking for is Time.zone.parse. First use DateTime.strptime to parse the string, then run it through Time.zone.parse to set the zone. Check out the following console output:
> Time.zone
=> (GMT-06:00) Central Time (US & Canada)
> input_string = "10/12/12 00:00"
> input_format = "%m/%d/%y %H:%M"
> date_with_wrong_zone = DateTime.strptime(input_string, input_format)
=> Fri, 12 Oct 2012 00:00:00 +0000
> correct_date = Time.zone.parse(date_with_wrong_zone.strftime('%Y-%m-%d %H:%M:%S'))
=> Fri, 12 Oct 2012 00:00:00 CDT -05:00
Notice that even though Time.zone's offset is -6 (CST), the end result's offset is -5 (CDT).
Ok, here is the best way I've found to handle this so far. I created a utility method in a lib file.
# Returns a DateTime object in the specified timezone
def self.parse_to_date(date_string, num_hours, timezone)
if timezone.is_a? String
timezone = ActiveSupport::TimeZone[timezone]
end
result = nil
#Chronic.time_class = timezone # Trying out chronic time zone support - so far it doesn't work
the_date = Chronic.parse date_string
if the_date
# Format the date into something that TimeZone can definitely parse
date_string = the_date.strftime("%Y-%m-%d")
result = timezone.parse(date_string) + num_hours.to_f.hours
end
result
end
Note that I add hours onto the time manually because Chronic.parse wasn't as robust as I liked in parsing times - it failed when no trailing zeros were added to a time, for example, as in 8:0 instead of 8:00.
I hope this is useful to someone. Parsing date/time/timzone strings into a valid date seems to be a very common thing, but I was unable to find any parsing code that incorporated all three together.