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()
Related
Using IDataReader, I need to get time values so that I can get the timespan. In my SQL database, these values are time(7). I am getting an error - InvalidCast Exception was unhandled by user code. Here is my code (and I know I have to rewrite the Select statement):
commandvct.CommandText = "Select begindateoff, enddateoff, begintimeoff, endtimeoff, allday_yesno from tblworkhours where Employee = " & rve & " and workcode = 2"
commandvct.CommandType = CommandType.Text
commandvct.Connection = sqlConnection
sqlConnection.Open()
Using reader As IDataReader = commandvct.ExecuteReader
While reader.Read()
Dim begdate As Date = reader.GetDateTime(0)
Dim enddate As Date = reader.GetDateTime(1)
If begdate = enddate Then
Dim allday As Boolean = reader.GetBoolean(4)
'If all day, add 8 hours
If (allday = True) Then
workinghoursv = workinghoursv + 8 'Change me to your needs.
TextBoxva2.Text = workinghoursv
'If not all day, add difference between times
Else
'tried defining it as datetime and as string - neither work
Dim begtime As String = reader.GetDateTime(2)
Dim endtime As DateTime = reader.GetDateTime(3)
Dim diffv As TimeSpan = (endtime).Subtract(begtime)
Dim diff2v As Decimal = (Convert.ToDecimal(diffv.TotalMinutes)) / 60
workinghoursv = workinghoursv + diff2v
TextBoxva2.Text = workinghoursv
End If
I am having the above exception come up when executing my method (code below):
Sub loadDates(ByVal newDate As Date)
Dim dayCount As Integer = 0
conn = New SqlConnection(connectionString)
ds = New DataSet("Holiday")
sql = New SqlCommand("SELECT * FROM Holiday", conn)
Dim da As SqlDataAdapter = New SqlDataAdapter("SELECT date FROM Holiday", conn)
conn.Open()
da.FillSchema(ds, SchemaType.Source, "Holiday")
da.Fill(ds, "Holiday")
Dim tblHoliday As DataTable
tblHoliday = ds.Tables("Holiday")
For Each row As DataRow In tblHoliday.Rows
If Not row Is Nothing Then
For items As Integer = 0 To tblHoliday.Rows.Count Step 1
Dim dateValue As Date = row.Item(0)
If newDate = dateValue Then
dayCount += 1
End If
Exit For
Next
End If
Next
conn.Close()
If newDate.DayOfWeek = DayOfWeek.Saturday Or newDate.DayOfWeek = DayOfWeek.Sunday Then
dayCount = 2
End If
For days As Integer = dayCount To 0 Step -1
Dim dropDates As Date = New Date(newDate.Year, newDate.Month, newDate.Day - days)
ddDate.Items.Add(dropDates.ToShortDateString())
Next
End Sub
I've narrowed down the problem to being within the For Each loop in the method, when I enter in a date and it skips over the loop it works without a problem. When it has to go through the loop it crashes on the line:
Dim dropDates As Date = New Date(newDate.Year, newDate.Month, newDate.Day - days)
Any ideas?
Subtract the days from the date instead of from the day component, that way the day compontent doesn't get out of range:
Dim dropDates As Date = New Date(newDate.Year, newDate.Month, newDate.Day).AddDays(-days)
If newDate doesn't have any time component component that you are getting rid of by using that Date constructor, you can just use newDate directly:
Dim dropDates As Date = newDate.AddDays(-days)
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
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'm doing the following query to check if the current month is the same as the SQL field "Start".
If Today.Month = CDate(rsData("Start")).Month Then
What I'd like to do is switch it so that it will check within a 30 day period rather than identify the current month? Any ideas on how to do this?
If Date.Today.AddDays(-30) >= CDate(rsData("Start"))
' start date not older than 30 days '
End If
or if you have a variable date:
var minBoundary = New Date(2011,1,1)
var maxBoundary = New Date(2012,1,1)
var startDate = CDate(rsData("Start"))
If startDate >= MinBoundary AndAlso startDate <= maxBoundary
' start date between two dates '
End If
I believe in this case you would want to use the AddDays method of DateTime.
Dim mydate as DateTime = CDate(rsData("Start"))
Dim checkdate as DateTime = mydate.AddDays(30)