How to get substring date (year) using vb.net? - vb.net

I've been doing this, is it possible to get the middle string in datetime.
Example :
Basically my string return this '12/23/2015 12:00:00 AM'.
However, what i want is actually '15' from the year 2015.
I've been using something like this 'strDate = strDate.Substring(0, 9)',
which sometimes return me wrong when the date is something like this '1/2/2015 12:00:00 AM'
Can someone help me on this, i am not sure if we can get the middle.

Don't use string methods for this, use DateTime.TryParseExact:
Dim str = "12/23/2015 12:00:00 AM"
Dim dt As DateTime
If DateTime.TryParseExact(str, "MM'/'dd'/'yyyy hh:mm:ss tt", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt) Then
Dim yearOnly As String = dt.ToString("yy") ' 15
End If
Update: with CultureInfo.InvariantCulture and this format you don't even need to use TryParseExact, you can use Parse/TryParse immediately:
DateTime.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.None, dt)
MSDN: Custom Date and Time Format Strings, The "yy" Custom Format Specifier

If you really want to treat it as just strings, you can get it by splitting twice. First on the space, then on the slashes:
Dim justDate as String = strDate.Split(" ")(0) 'contains "12/23/2015"
Dim justYear as String = justDate.Split("/")(2) 'contains "2015"
Dim partYear as String = justYear.Substring(2,2) 'contains "15"
or all in one line:
Dim partYear as String = strDate.Split(" ")(0).Split("/")(2).Substring(2,2)

Try this
dim dateStr as string
dateStr = "12/23/2015 12:00:00 AM"
dim dateobj as new DateTime
dateobj = DateTime.Parse(dateStr)
dim year as integer
year = dateobj.Year
dim youWantStr = year.ToString().Substring(2)
Console.WriteLine(youWantStr)

Related

Difficulty with formatting a string to parse to date in vb.net

dim dt as string= "03/22/20 20:12:27.320"
Dim tempDate As DateTime = DateTime.ParseExact(dt, "MM/dd/yy hh:mm:ss.fff", CultureInfo.GetCultureInfo("en-US").DateTimeFormat)
This gives error sometimes : System.FormatException: 'String was not recognized as a valid DateTime.'
Why? It seems to be formatted properly. VB.net 4.6.1 framework
"hh" is 12-hour format. 20 is obviously not a valid hour in 12-hour format. If you want 24-hour format then use "HH".
Have you tried to use CDate() function?
dim dt as string= "03/22/20 20:12:27.320"
dim tempDate as Datetime = Cdate(dt)
or
dim tempDate as Datetime = Cdate("03/22/20 20:12:27.320")

VB.net yyyymmdd format needs to parse as date [duplicate]

I am developing asp.net site using vb framework 3.5.
Im having difficulties converting string data into Date
I tried using cdate function,
I have a variable sdate which is a string variable and date is stored in it which comes from textbox as dd/mm/yyyy now i want to convert this string into a Date variable as i need to perform the operations as Add a day or Subtract a day.
Please guide me how to go about this. i get the error on 3rd line as,String was not recognized as a valid DateTime. I have tried to do as follows but the error comes
Dim sdate As String
Dim expenddt As Date
expenddt = Date.Parse(edate)
expenddt = expenddt.AddDays(-1)
But i get the error as
Conversion from String to type Date is not valid.
How can I get a Date from the string?
You should have to use Date.ParseExact or Date.TryParseExact with correct format string.
Dim edate = "10/12/2009"
Dim expenddt As Date = Date.ParseExact(edate, "dd/MM/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo)
OR
Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date = Date.ParseExact(edate, format,
System.Globalization.DateTimeFormatInfo.InvariantInfo,
Globalization.DateTimeStyles.None)
OR
Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date
Date.TryParseExact(edate, format,
System.Globalization.DateTimeFormatInfo.InvariantInfo,
Globalization.DateTimeStyles.None, expenddt)
Nobody mentioned this, but in some cases the other method fails to recognize the datetime...
You can try this instead, which will convert the specified string representation of a date and time to an equivalent date and time value
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
MessageBox.Show(oDate.Day + " " + oDate.Month + " " + oDate.Year );
Try to see if the following code helps you:
Dim iDate As String = "05/05/2005"
Dim oDate As DateTime = Convert.ToDateTime(iDate)
Try converting date like this:
Dim expenddt as Date = Date.ParseExact(edate, "dd/mm/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
Hope this helps.
Try to use DateTime.ParseExact method, in which you can specify both of datetime mask and original parsed string.
You can read about it here: MSDN: DateTime.ParseExact

VB.NET String Data Manipulation for DateTimePicker

I want to manipulate my String data for example I have a x="20140118"
to y="01/18/2014" how can I do it? I need it for the value in DateTimePicker on VB.NET.
Thanks
DateTimePicker.Value wants a DateTime not a string. So you need to parse it:
Dim dt As DateTime = DateTime.ParseExact("20140118", "yyyyMMdd", CultureInfo.InvariantCulture)
dateTimePicker1.Value = dt
DateTime.ParseExact
Custom Date and Time Format Strings
However, just for the sake of completeness, if you need a string 01/18/2014 from the DateTime you can use DateTime.ToString:
Dim date As String = dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
If that is your local date format you could also use these more concise approaches:
)
Dim date As String = dt.ToShortDateString()
)
Dim date As String = dt.ToString("d")

Convert a string to a datetime

I am developing asp.net site using vb framework 3.5.
Im having difficulties converting string data into Date
I tried using cdate function,
I have a variable sdate which is a string variable and date is stored in it which comes from textbox as dd/mm/yyyy now i want to convert this string into a Date variable as i need to perform the operations as Add a day or Subtract a day.
Please guide me how to go about this. i get the error on 3rd line as,String was not recognized as a valid DateTime. I have tried to do as follows but the error comes
Dim sdate As String
Dim expenddt As Date
expenddt = Date.Parse(edate)
expenddt = expenddt.AddDays(-1)
But i get the error as
Conversion from String to type Date is not valid.
How can I get a Date from the string?
You should have to use Date.ParseExact or Date.TryParseExact with correct format string.
Dim edate = "10/12/2009"
Dim expenddt As Date = Date.ParseExact(edate, "dd/MM/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo)
OR
Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date = Date.ParseExact(edate, format,
System.Globalization.DateTimeFormatInfo.InvariantInfo,
Globalization.DateTimeStyles.None)
OR
Dim format() = {"dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy"}
Dim expenddt As Date
Date.TryParseExact(edate, format,
System.Globalization.DateTimeFormatInfo.InvariantInfo,
Globalization.DateTimeStyles.None, expenddt)
Nobody mentioned this, but in some cases the other method fails to recognize the datetime...
You can try this instead, which will convert the specified string representation of a date and time to an equivalent date and time value
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
MessageBox.Show(oDate.Day + " " + oDate.Month + " " + oDate.Year );
Try to see if the following code helps you:
Dim iDate As String = "05/05/2005"
Dim oDate As DateTime = Convert.ToDateTime(iDate)
Try converting date like this:
Dim expenddt as Date = Date.ParseExact(edate, "dd/mm/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
Hope this helps.
Try to use DateTime.ParseExact method, in which you can specify both of datetime mask and original parsed string.
You can read about it here: MSDN: DateTime.ParseExact

How can I convert two strings to DateTimes and compare them in VB.NET?

String comes back from the database with a format: '00/00/0000' I need to then compare it to a date that the user has entered in the same format. How do I make the conversion and compare the two dates?
Use the static ParseExact method on the DateTime structure to convert the string. You will also pass the format you want, either dd/MM/yyyy or MM/dd/yyyy depending on what format you want (the example of 00/00/0000 doesn't give any indication of what format applies for you).
You can use
Dim dateA = DateTime.ParseExact(firstDateString, #"dd\/MM\/yyyy", Null)
Dim dateB = DateTime.ParseExact(secondDateString, #"dd\/MM\/yyyy", Null)
Dim areEqual = (dateA = dateB);
Assuming that your date format is day/month/year.
If it's month/day/year just swap dd and MM
Try something like this:
String.Compare("00/00/0000", dateTime.ToString("MM/dd/yyyy"))
But perhaps a better approach would be to do this:
DateTime.Equals(yourDateTime, DateTime.Parse(databaseDateTime));
Try the following
Dim date1 = CDate(firstDateString)
Dim date2 = CDate(secondDateString)
Dim comp = date1 = date2
When you say compare, are you trying to analysis if the dates are the same (to the day) or within a period of a few days ? If to compare if the dates are the same then you can just compare the string or use the date.equals (as mentioned in posts before this one) , if you are trying to determine with a range you will have to use date compare
Dim lDate1 As String = "29/03/2009"
Dim lDate2 As String = "30/03/2009"
Dim lPeriod As Int16 = 7
If lDate1 = lDate2 Then
'** Dates the same
End If
If Date.Equals(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) Then
'** The same
End If
If Date.Compare(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) > (lPeriod * -1) And Date.Compare(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) < lPeriod Then
'** Within the period
End If