What is the date format to convert from string to date - vb.net

I have below code to get StringToDate as 04/29/2019, but when use CDate function it's giving me #4/29/2019 12:00:00 AM# value. which date format i have to use to get 04/29/2019
Dim StringToDate As Date
Dim strNewDate As String = "04/29/2019"
StringToDate = CDate(strNewDate)

You could use DateTime.TryParse
Dim MyDate As DateTime = Nothing
Dim strNewDate As String = "04/29/2019"
DateTime.TryParse(strNewDate, MyDate)
Edit (misread your question)-
A DateTime holds value and not String. To get what you asking after parsing the date you would need to convert it back to a string with a format method or use the following:
MyDate.ToShortDateString

The way a date is represented is by what the region settings of your OS have setup.
For example, if you were to open the region settings in Control Panel you would find a format for Short Date which by what you're describing is probably M/d/yyyy. Inside the dropdown are other formats you can set and if you were to set it to MM/dd/yyyy you would see that your same code would now display 04/29/2019. But Don't Do This as it only affects your machine. Instead, understand that a date is only number, and you can control how it is displayed, the same way Windows just did from my example. A perfect example of this is that you could set a DateTime variable in many different ways. Here's a few:
Dim dt1 As DateTime = #2019-12-14#
Dim dt2 As DateTime = #1/08/2019#
Dim dt3 As DateTime = #03/03/2019#
Dim dt4 As DateTime = #4/4/2019#
The results would be
12/14/2019 12:00:00 AM
1/8/2019 12:00:00 AM
3/3/2019 12:00:00 AM
4/4/2019 12:00:00 AM
because that is how windows is set up to show me a DateTime as a string.
When you want to display this date to the user in a different format you can simply supply a format to the .ToString() method or use one of the DateTime string conversion methods (ie, .ToShortDateString, .ToLongDateString, etc) You can show the date to the user in a more preferable format.
Taking the last example (dt4) you could use
Console.WriteLine(dt4.ToShortDateString) 'and show 4/4/2019
Console.WriteLine(dt4.ToLongDateString) 'and show Thursday, April 4, 2019
Console.WriteLine(dt4.ToString("MM/dd/yyyy")) 'and show 04/04/2019

Related

How to compare two datetime value instead of string using VB.Net

How to compare two datetime values?
For example as per below condition, I have FDatetimes value is string coming (i.e 2021-06-21 17:00:30Z) from database and SDatetimes value should be Datetime.UtcNow. So my requirement is both left and right side always compare with datetime instead of string.
Also I have tried to use Convert.ToDateTime(FDatetimes). But there is one issue here. Whenever I have used the above function, That time the time is getting changed to 2021-06-21 10:00:30Z. My main aim is to compare both field value as DateTime instead of String. Please help in VB.Net
If FDatetimes < SDatetimes Then
'My logic
End If
So to resolve DST issues I did this (which I ran at 9:20AM local, 14:20 UTC on 23 Jun 2021)
Dim SDatetimes As DateTime = DateTime.UtcNow
Dim FDatetimes As String = "2021-06-23 15:20:30Z"
Dim FDT As DateTime = DateTime.ParseExact(FDatetimes,
"yyyy-MM-dd HH:mm:ssZ",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.AdjustToUniversal Or
Globalization.DateTimeStyles.AssumeUniversal)
Debug.WriteLine(SDatetimes.ToString)
Debug.WriteLine(FDT.ToString)
If FDT < SDatetimes Then
'My logic
Stop
End If

Compare code date to sql date ASP NET

I get the error about convert type date when i try to compare this :
Page Load :
Dim DataCorrenteLoad As String = DateTime.Now.ToString("dd-MM-yyyy")
Label2.Text = DataCorrenteLoad
and in SqlDataSource the where condition is that the FinishDate >= Today date
How can i solve this situation ? Should i change the format dd-mm-yyyy ? Or what ?
Thanks.
Seems, you want to compare dates, but you kept your focus on string representation of a date. This is not the same!
Take a look at example:
Dim DatabaseDate As DateTime = DateTime.Today.AddDays(-5)
Dim CurrentDate As DateTime = DateTime.Today
'case 1: compare and display date in current culture - depending on regional settings
Console.WriteLine("{0} {1} equal to {2}", DatabaseDate.ToString("d"), If(DatabaseDate=CurrentDate, "is", "is not") , CurrentDate.ToString("d"))
'case 2: change string representation of date:
'compare dates in France and German format
Dim cu1 As Globalization.CultureInfo = New Globalization.CultureInfo("Fr-fr")
Dim cu2 As Globalization.CultureInfo = New Globalization.CultureInfo("De-de")
DatabaseDate = DateTime.Today
Console.WriteLine("{0} {1} equal to {2}", DatabaseDate.ToString("d", cu1), If(DatabaseDate=CurrentDate, "is", "is not") , CurrentDate.ToString("d", cu2))
Above code returns:
'case 1
2017-06-08 is not equal to 2017-06-13
'case 2
13/06/2017 is equal to 13.06.2017
Conclusion:
Depending on OS regional settings, the date format may differ, but the date is still the same!
For further details, i'd strongly recommend to read these:
Standard Date and Time Format Strings
Custom Date and Time Format Strings
Parsing Date and Time Strings in .NET
Design and Implementation Guidelines for Web Clients
Formatting Date and Time for a Specific Culture
Label2.Text= System.DateTime.Now.ToString("dd-mm-yyyy");
use this it will work System.DateTime.Now will show current time when you use To strong("dd-mm-yyyy") it will convert it in to string and set it's formate which u set under double quotes.

Convert string to date with format (VB.net)

I have a string with a date in this format. "24-11-2015" or "dd-MM-yyyy". I need to convert this to a date with this format. "2015-11-24" or "yyyy-MM-dd".
I've tried several ways, and all of them rely on my system format for this to work. If i run my program on a computer with english time format this works.
Date.ParseExact("24-11-2015", "dd-MM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
But if i run it on a computer running danish format i get error because there is no 24th month. How do i make the date format independent of my system time format.
Solution:
Dim dateTime As String = "24-11-2015"
Dim dt As DateTime = Convert.ToDateTime(dateTime)
Console.WriteLine("{0}-{1}-{2}", dt.Year, dt.Month.ToString("D2"), dt.Day.ToString("D2"))
Solution 2:
Dim dateTime As String = "24-11-2015"
Dim dt As DateTime = Convert.ToDateTime(dateTime)
Dim format As String = "yyyy-MM-dd"
Dim str As String = dt.ToString(format)
Console.WriteLine(str)
You can also try Formatting Date and Time for a Specific Culture.

Date format from database gets wrong date in vb.net

I'm taking in data from a SQL database and now I'm trying to convert the data to a proper date variable in VB.NET but I don't get the proper format.
Data from the database:
28-FEB-14 01:00:00:0
28-FEB-14 13:00:00:0
The date I get when trying to convert it:
2028-02-14
2028-02-14 12:00:00
The code that is doing the conversion:
Dim theDate As DateTime
Dim try1 As Date
For i As Integer = 0 To batchCount - 1
If Date.TryParse(dv(i)(0), theDate) = True Then
try1 = theDate.ToUniversalTime()
End If
Would appreciate all the help I can get.
Firstly, date values in your database should be date types, so when you read them in, you read them into a Date type variable.
If you then want to format that date you can use a ToString overload
Dim theDate as DateTime = dr.item("date")
Debug.Writeline(theDate.ToString("yyyy-MM-dd")
Secondly if the date is being stored as a string field in the database (you should not do this but you may have no control over this) it should be in a specific format, so you can use ParseExact to convert this into a date:
Dim dateString As String
dateString = DateTime.ParseExact(dr.item("Date"), "dd-MMM-yy HH:mm:ss.f", CultureInfo.CurrentCulture)

How to get a date field from MM/dd/yyyy to yyyy/MM/dd in vb.net

I need to get a date field from MM/dd/yyyy to yyyy/MM/dd in vb.net but it should still be a date field afterward so that I can match it against a date in a database.
At the moment all I'm managing to do is to change it to a string in that format.
I tried this type of code which also did not work.
DateTime.Parse(yourDataAsAString).ToString("yyyy-MM-dd")
fromDeString = String.Format("{0:yyyy/MM/dd}", aDate)
fromDate = Format("{0:yyyy/MM/dd}", aDate)
Any help would be much apreciated, thanks
You're not understanding that a date object does not store the digits in any particular format. The only way to get the digits formatted in the order you want is to convert it to a string. Why do you need to compare them in a particular format? A date is a date no matter how it is formatted. 12/15/78 == 1978/12/15.
If you are not able to compare dates from the DB against a date object in VB, it is likely that the date you are comparing to in the database is being returned to you in string format, in which case you should covert it to a date object for comparison.
Dim sDate As String = "2009/12/15" 'Get the date from the database and store it as a string
Dim dDate As New Date(2009, 12, 15) 'Your date object, set to whatever date you want to compare against
Select Case Date.Compare(dDate, Date.Parse(sDate))
Case 0
'The dates are equal
Case Is > 0
'The date in the database is less
Case Is < 0
'The date in the database is greater
End Select
Here's a sample module demonstrating the functionality you desire.
Imports System.Globalization
Module Module1
Sub Main()
Dim culture As New CultureInfo("en-us", True)
Dim mmDDyy As String = "10/23/2009"
Dim realDate As Date = Date.ParseExact(mmDDyy, "mm/dd/yyyy", culture)
Dim yyMMdd As String = realDate.ToString("yyyy/MM/dd")
End Sub
End Module
Hope this helps.
Kind Regards
Noel
Your second should actually work. Instead just try:
dim chislo as date = date.now dim message As String = $"
Today`s Date: {String.Format("{0:dddd, dd/MM/yyyy}", Chislo)} "
MsgBox(message)