VB.NET compare date time from string format - vb.net

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

Related

How to convert "Wed Jan 26 2022" to date in sql?

I have varchar like this "Wed Jan 26 2022"
I need to convert this to date in sql. How can i do this
for Sql Server:
convert(date, substring('Wed Jan 26 2022',5,11),9)
we ignore the Day name (superfluous), and convert the rest using format 9 indicating Mon dd yyyy format.
SQL*plus server (Here's my code) -
If I am right, you want such type of string which is an invalid one to convert that into a valid one so that you can store valid data into the database. then this code you can use->
SELECT TO_DATE('WED JAN 26 2022','DY MON DD YYYY')FROM DUAL;
(Explanation)->
Code will convert invalid date datatype to a valid date data type which is used in Oracle(SQL).
DY = Abbreviated Week Day
DD = Month day indicator
MON = Abbreviated month
YYYY = Four-digit year indicator

Convert Time and Date in string to date VB.net

I'm using RSS feeds from multiple sources and I'm sorting the feeds by date. However, the dates I receive are in different time zones.
This is the format of some of the date strings that I get:
Wed 08 Dec 2021 01:40:31 -0500
Wed 08 Dec 2021 11:11:19 -0600
The "-500' indicates the time zone which is UTC-5. I need to use that "-0500" to convert this string into a date and the date needs to be only in UTC.
Basically from "Wed 08 Dec 2021 01:40:31 -0500" to "12/08/2021 06:40:31"
I've managed to split the string up to sort by date but because of the time difference, it doesn't really help.
Is there coding which I can use to convert the string as-is into date and only UTC time? Or is there just a place where I can start?
Thank you in advance.
Using DateTime.ParseExact, you specify a format to convert from, and that can include "zzz" for the timezone offset. You can also specify that you want the result in UTC:
Dim s = "Wed 08 Dec 2021 01:40:31 -0500"
Dim d = DateTime.ParseExact(s, "ddd dd MMM yyyy HH:mm:ss zzz", Nothing, Globalization.DateTimeStyles.AdjustToUniversal)
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"))
Console.WriteLine(d.Kind.ToString())
Outputs:
2021-12-08 06:40:31
Utc
Of course, format the result as you desire.
Alternatively, if you need to, you can keep the offset by using a DateTimeOffset structure and adjust it to UTC for representation as a string:
Dim s = "Wed 08 Dec 2021 01:40:31 -0500"
Dim d = DateTimeOffset.ParseExact(s, "ddd dd MMM yyyy HH:mm:ss zzz", Nothing)
Console.WriteLine(d.ToUniversalTime.ToString("yyyy-MM-dd HH:mm:ss"))
Outputs:
2021-12-08 06:40:31

today's date filter showing next day data in table chart

When i select date as sept 16,i get to see sept 16 6am and sept 17 data until 5:59 AM.
It is treating 24 hours from sept 16 am to sept 17 6am.
Is there any issue with date field or report filter issue?
BigQuery's date functions work based on UTC timezone.
The UI you are using probably converts them into your local timezone, and you are seeing the 6 hours difference.
For visibility purposes I am posting my comment as an answer:
As #Pentium10 mentioned:
BigQuery's date functions work based on UTC timezone. The UI you are using probably converts them into your local timezone, and you are seeing the 6 hours difference.
According to this Data Studio uses UTC standard time, but if your data set does not use UTC you can use the TODATE function to convert the date field to UTC.

Conditional DateTime.ParseExact format

I am parsing a date from a bank deposit report, and the format is like this:
Jul 9 2015
Jun 20 2015
Basically MMM dd yyyy except that the single digit day does not contain a leading zero. Is there a simple way to do conditional formatting in DateTime.ParseExact()? Or will I have to pre-process the date string and either add the leading zero or remove the extra space?? Here is what works for the single digit day dates:
Dim dtDepositDate As DateTime
dtDepositDate = DateTime.ParseExact(strDate, "MMM d yyyy", CultureInfo.InvariantCulture)
and obviously, MMM dd yyyy would work for the two digit dates, but would not work for the single digit dates with an extra space in between.
For single/double digit day part
Use single d which is good for both single and double digits day value.
Having single d would effect the values if the DateTime is converted to string. As far as parsing is concerned, it will work for both single and double digits day values, like 01 , 1 , 11, 20 etc. The same is true for M, H, m, specifier for Month, Hour Minutes etc.
For multiple spaces
For multiple spaces use DateTimeStyles.AllowWhiteSpaces in parsing.
DateTime dt = DateTime.ParseExact("Jul 9 2015", "MMM d yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces);
or for double digit day part:
DateTime dt = DateTime.ParseExact("Jun 20 2015", "MMM d yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces);

Create a utc date in vb.net to set in mongodb

I'm having a bit of trouble with the mongodb c# driver, in that it seems to be converting all my dates to a UTC form.
I have
Dim cDate as Date
Dim year as integer = 2012
Dim month as integer = 12
Dim day as integer = 21
cDate = New Date(year, month, day)
However putting it into a mongodb database via the C# driver seems to convert it to UTC so all of a sudden its a different day now because its now 11:00 PM 20th December 2012. Not exactly what I wanted!
Is there a way I can create the New Date(year, month, date such that its an UTC mode to begin with? So if i did cDate.utcNow I would get the same thing as cDate, in essence cDate.utcNow = cDate?
I have tried all sorts of stuff with the driver only to run into a brick wall such as using the DateTimeSerializationOptions.Defaults to no avail nothing happens!
To create your date as a UTC date, just specify the Kind parameter in the constructor along with the hours, minutes, and seconds:
cDate = New Date(year, month, day, 0, 0, 0, DateTimeKind.Utc)