Compare code date to sql date ASP NET - sql

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.

Related

What is the date format to convert from string to date

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

VB.NET - Unable to enforce two digit day and month when converting string to Date

I am having difficulty taking a string and converting it to a vb.net Date object, while enforcing two digit day and month. Please consider the following form example, using today's date. (02/01/2019)
Dim myDate As Date = Date.Now
Dim myDateString = String.Format("{0:D2}/{1:D2}/{2:D4}", myDate.Month, myDate.Day, myDate.Year)
myDate = DateTime.ParseExact(myDateString, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None)
Label1.Text = myDate 'This will show "2/1/2019"
Label2.Text = myDateString 'This will show "02/01/2019"
This situation leaves Label1.Text as "2/1/2019", but Label2.Text as "02/01/2019". No matter what I have tried, it appears that the actual conversion from the correctly formatted String into a Date object will remove these zeros. Does anyone have any thoughts as to how I can enforce a "MM/dd/yyyy" format when converting to a Date object?
Thank you in advance,
You should consider that a DateTime variable has no format. It is just a number expressing the Ticks elapsed from the starting DateTime.MinValue (1/1/0001).
It has no memory that you have built it using a particular formatting parser.
So, when you assign a date to a string like you do in your
Label1.Text = myDate
then you are asking the ToString representation of that date. The output of this method without a formatting parameter is whatever your locale settings decide it to be. If you want to force the desidered output you need to tell the date's ToString method in what format you want the output
Label1.Text = myDate.ToString("MM/dd/yyyy")

Why converting a date to a string changes the order?

Here is a peculiar result generated from this simple code:
Dim TodaysDate As Date = Date.Today ' Returns #8/12/2014#
Dim StringDate As String = TodaysDate ' Returns 12/08/2014
Dim AnotherStringDate As String = TodaysDate.ToString ' Returns 12/08/2014 00:00:00
What is going on? Why the string reversed the result, although I did not even used any conversion command?
Change Option Strict to On, then you get a compiler error instead of weird conversions. In general, a Date is not a String but it can be represented as one.
If i remember correctly VB uses helper methods with Option Strict Off to convert the type automatically which are sitting in the namespace Microsoft.VisualBasic.CompilerServices.
I've looked at the source-code (with ILSpy) and found a class StringType which has a method FromDate:
' Microsoft.VisualBasic.CompilerServices.StringType
Public Shared Function FromDate(Value As DateTime) As String
Dim ticks As Long = Value.TimeOfDay.Ticks
If ticks = Value.Ticks OrElse (Value.Year = 1899 AndAlso Value.Month = 12 AndAlso Value.Day = 30) Then
Return Value.ToString("T", Nothing)
End If
If ticks = 0L Then
Return Value.ToString("d", Nothing)
End If
Return Value.ToString("G", Nothing)
End Function
Since your Date was derived from Date.Today which is the current date without time, the code will be executed which starts with If ticks = 0L Then(ticks is from the time of the day).
This returns:
Value.ToString("d", Nothing)
which is the same as Date.ToShortDateString. The format is derived from your current culture since null is passed as CultureInfo. So obviously your current culture uses / as date separator.
You could also force this format by using:
Dim StringDate As String = TodaysDate.ToString("d")
StringDate = TodaysDate.ToShortdateString()
StringDate = TodaysDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
The last option also ensures that this format is used even if your current culture is different.
A date is stored internally as number. And a date/datetime variable doesn't really know or care what actual date it represents; it is just a number (decimal). The string representation is for our eyes only. When you convert a date/datetime variable to string type, it is converted in the format specified by our machine locale settings, unless we specifically tell it to convert it to some other format.
I am assuming that the date/time settings on your PC is in the dd/mm/yyyy (indian format) format rather than the default mm/dd/yyyy (US format).
So this is what is actually happening:
' You store today's date in a Date variable and it returns #8/12/2014# i.e. 12-Aug-2014
Dim TodaysDate As Date = Date.Today
' You convert it to string. Since you didn't sepecify the format, it picks up the fromat settings from your PC regional settings.
' Returns 12/08/2014 which means 12-Aug-2014 (in your locale)
' You didn't do .ToString, but that's implicit, unless you set OPTION STRICT ON, in which case it will force you to put .ToString
Dim StringDate As String = TodaysDate
' Same argument as above.
Dim AnotherStringDate As String = TodaysDate.ToString ' Returns 12/08/2014 00:00:00
As for the question why converting a date to a string changes the order? The answer is that it doesnt.
When you convert to string, your local culture format is used, as explained by Tim. That's not teh case with Date literals in VB.
Date literals - values between hashes such as #02/11/2014# - are always displayed in InvariantCulture or "m/d/y" pattern. This is likely because when you wish to create a date using a literal, you must specify the Day and Month in that format. See MSDN:
Date literals must be in the format #m/d/yyyy# or they are invalid.
So, it would make no sense to display the date to you in one pattern but require you to create them using a different pattern. This is just the way VB works (in part because the legacy functions have no notion of cultures beyond the local one in use). This is a VB-ism as seen in this VB vs C# date display in Visual Studio:
The culture is set to fr-FR.
But your strings ultimately represent the same date - they can make a roundtrip:
Dim TodaysDate As Date = Date.Today ' Returns #8/12/2014#
Dim AnotherStringDate As String = TodaysDate.ToString
Dim mydt As DateTime = DateTime.Parse(AnotherStringDate)
If mydt = TodaysDate Then
Console.Beep()
End If
It will beep because the same DateTime is created from the string even though it may display in the pattern of "d/m/y" for yur culture.

Convert string date to show 2 year digits

i have a string which has a value of "08-06-2008". I want the result to look like "08-06-08". Is there a way to do this? I've tried CDate but that only gives me 8/06/2008 which doesnt resolve the issue.
Parse it to Date and back to string:
Dim dt As Date = Date.ParseExact("08-06-2008", "MM-dd-yyyy", CultureInfo.InvariantCulture)
Dim result As String = dt.ToString("MM-dd-yy", CultureInfo.InvariantCulture)
Since that is a normal format you could also omit the format string and use Date.Parse directly:
Dim dt As Date = Date.Parse("08-06-2008", CultureInfo.InvariantCulture)
I have used CultureInfo.InvariantCulture to avoid localization issues, normally your current culture is used in Parse/ParseExact and ToString.
See: Custom Date and Time Format Strings
Firstly, avoid CDate. It is a hangover from VB6, and is almost certainly less efficient than using the .net equivalent.
The following should give you the right answer:
string value = DateTime.ParseExact("08-06-2008", "dd-MM-yyyy").ToString("dd-MM-yy")
Note that your existing date format is ambiguous, so I've been unable to tell if you're meaning the 6th of August or the 8th of June - this is why I've used ParseExact over Parse. ParseExact is also slightly more efficient, as it tells the framework which format to use, rather than it having to guess.
Try this
Dim FormatString As String = ""
Dim SampleDate As DateTime
SampleDate = Now()
FormatString = Format(SampleDate,"dd-MM-yy")

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)