I am using ASP.NET Visual Basic.
I have an integer field in my database which I need to read as a date e.g. 20130813. But at the moment it says I cannot convert to datetime.
Is there a way to split the integer up into DD, MM, YYYY so I could even store those in variables to then use to create a date. Or a simple way of just converting to date.
Thanks for your time.
Try this:
Dim provider As CultureInfo = CultureInfo.CurrentCulture
Dim sData As String = "20130813"
Dim myDate As Date = Date.ParseExact(sData, "yyyyMMdd", provider)
You can convert your number to string easily, if needed:
Dim myNum As Integer = 20130813
sData = myNum.ToString()
See MSDN Documentation for more information.
As you're starting with a number, you could either convert it to a string and use DateTime.TryParseExact, or use a bit of integer arithmetic to split it up:
Dim n As Integer = 20130813
Dim yr As Integer = n \ 10000
Dim mon As Integer = (n - 10000 * yr) \ 100
Dim d As Integer = n Mod 100
Dim dt As New DateTime(yr, mon, d)
Quicker:
Dim d As Date = Date.FromOADate(41498)
(from Robert Shutt in experts-exchange.com)
Private Sub stringtodate(ByVal dateval As String)
Dim yy As String
Dim mm As String
Dim dd As String
Dim newdate As Date
yy = dateval.Substring(0, 4)
mm = dateval.Substring(4, 2)
dd = dateval.Substring(6, 2)
newdate =Convert.ToDateTime(dd & "/" & mm & "/" & yy)
MsgBox(newdate.ToString())
End Sub
Related
so i have this problem, i want to make specific number to become a day of month.
example: i had an output of label 5 now how to make it to become 05-12-2016 ? i just convert them into string like this
dim testing as string = ""
testing = Now.Year.ToString & Right("0" & Now.Month.ToString, 2) & Right("0" & "5" 2)
output :
2016-11-05
and if i use string, i cannot compare it with date.Now
Thanks in advance
DateTime has a constructor that takes the year, month and day of month as parameters that you can use to create an instance:
Dim dayOfMonth As String = "5"
Dim myDate = New Date(Date.Now.Year, Date.Now.Month, CInt(dayOfMonth))
You can use the ToString method to format the date however you need:
Dim format1 As String = myDate.ToString("yyyy-MM-dd")
Dim format2 As String = myDate.ToString("dd-MM-yyyy")
This question already has answers here:
Split String in VB.net
(2 answers)
Closed 7 years ago.
I have a function in VB.Net which takes two arguments(dosage & no_days).
'dosage' have values of the 1-2-1 format.
I need to get the values as integer from the string: for example, if the string is 1-1-2, I need to get the values as 1,1,2. And sum of these three numbers need to multiply with no_days and return the result.
I need to know how can I split the string using regex "-" or any other logic for doing this.
Public Function calculateNoOfPeices(ByVal dosage As String, ByVal days As Integer) As String
Dim noOfPiece As Double = 0.0
If txt_dosage.Text.Length > 4 Then
' need logic to extract the values from the dosage
'and need to multiply with no_days
Else
'lbl_notif.Visible = False
noOfPiece = "-1"
End If
Return noOfPiece.ToString
End Function
Please help.
Using String.Split
Dim dosage As String = "1-2-1"
Dim IntValues() As String = dosage.Split("-")
Dim fValue As Double = Val(IntValues(0))
Dim sValue As Double = Val(IntValues(1))
Dim tValue As Double = Val(IntValues(2))
Using Regx
Dim pattern As String = "-"
Dim substrings() As String = Regex.Split(dosage, pattern)
Dim fValueR As Double = Val(substrings(0))
Dim sValueR As Double = Val(substrings(1))
Dim tValueR As Double = Val(substrings(2))
Or You can take the Sum using:
Dim overAllSum = dosage.Split("-").ToList().Where(Function(x) IsNumeric(x)).Sum(Function(y) Val(y))
How is it possible to calculate the percentage of days completed by using VB.NET?
The datareader takes project_start and project_finished, stored as Date() in SQL-Server-2012.
This is what I tried:
Dim StartDate As New Date(datareader("project_start"))
Dim FinishDate As New Date(datareader("project_finish"))
Dim Percentage As Date = Date.FromOADate(StartDate.DayOfYear) / Date.FromOADate(FinishDate.DayOfYear) / 100
But I get this error:
Operator '/' is not defined for types 'Date' and 'Date'.
You need to substract Dates and use TotalDays property. The example code below:
Dim start As DateTime = DateTime.Now.AddDays(-50)
Dim endDate As DateTime = DateTime.Now.AddDays(50)
Dim today As DateTime = DateTime.Now
Dim sumDays = (endDate - start).TotalDays
Dim daysToNow = (today - start).TotalDays
Dim percentage = daysToNow / sumDays * 100
Console.WriteLine(percentage)
Console.ReadLine()
Hi i have the following Code :
Dim CurrentDate
CurrentDate = Date()
Dim intHour
Dim intMinute
Dim intSecond
intHour = 17
intMinute = 0
intSecond = 0
Dim NewDate
Dim NewDate1
Dim NewDate2
NewDate = DatePart("yyyy", CurrentDate)
NewDate1 = DatePart("m", CurrentDate)
NewDate2 = DatePart("d", CurrentDate)
Dim Dates
Dates = DateSerial(NewDate, NewDate1, NewDate2)
Dim Time
Time = TimeSerial(intHour, intMonth, intSecond)
I have done something equal in VB:
Dim value As Date = Date.Now
Dim intHour As Integer
Dim intMinute As Integer
Dim intSecond As Integer
intHour = 17
intMinute = 0
intSecond = 0
Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond)
In VB i can do
Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond).
In my ASP Code i have Dates = DateSerial(NewDate, NewDate1, NewDate2) and Time = TimeSerial(intHour, intMonth, intSecond). How can i put them together as DateTime like in VB?
Two ways:
dim h,n,s
h = 17
n = 1
s = 2
dim t
t = timeserial(h,n,s)
dim d
d = date()
dim ts1
ts1 = dateadd("h",h, _
dateadd("n",n, _
dateadd("s",s, d )))
dim ts2
ts2 = d + T
Both produce the same output. The one with the additions has some gotchas depending on how close to day 0 you play it. I think the first way is "saner". As far as I recall VBScript just stored the date part in the integer or a float and the time part in the fraction part of a float (as parts of a 24 hour day, so 12:00 is 0.5), hence you can just add them together with +
I need to filter data based on a date range.
My table has a field Process date. I need to filter the records and display those in the range FromDate to ToDate.
How do I write a function in VB.NET which can help me filter the data.
Am I on the right track??
Why dont you do yourself a favor and DateTime.Parse your strings and use Date comparison operators
Something like
Function ObjectInRange(ByRef obj As Object, ByVal str1 As String, ByVal str2 As String) As Boolean
Dim date1 As DateTime = DateTime.Parse(str1)
Dim date2 As DateTime = DateTime.Parse(str2)
Dim inRange = False
For Each prop As PropertyInfo In obj.GetType().GetProperties()
Dim propVal = prop.GetValue(obj, Nothing)
If propVal Is Nothing Then
Continue For
End If
Dim propValString = Convert.ToString(propVal)
Dim propValDate = DateTime.Parse(propValString)
If propValDate.CompareTo(date1) > 0 And propValDate.CompareTo(date2) < 0 Then
inRange = True
Exit For
End If
Next
Return inRange
End Function