How can i minus two Dates asp.net - asp.net

How can i minus two Dates (LblExpirydate.Text - Label3.Text )
LblExpirydate.Text = String.Format("{0:dd/MM/yyyy}", dataReader(0))
Label3.Text = System.DateTime.Now.ToString(("dd/MM/yyyy"))
LblExpirydate.Text = 01/05/2013
Label3.Text = 01/04/2011

You can subtract one date from another to get a TimeSpan. You should not try to do date calculations on strings: your program is likely to fall foul of an assumed date format somewhere.
Dim dateFormat As String = "dd/MM/yyyy"
Dim rightNow As DateTime = DateTime.Now
Dim expiryDate As DateTime = rdr.GetDateTime(0)
Dim daysToExpiry As Integer = (expiryDate - rightNow).Days
LblExpirydate.Text = expiryDate.ToString(dateFormat)
Label3.Text = expiryDate.ToString(dateFormat)
LabelExpires.Text = daysToExpiry.ToString & " days"

Related

IDataReader - Get value of SQL time(7) field to use in calculations

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

The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

i am having problem with my retrieving of date after the date fall after 12. for example : if i click from the calander extender: 2/7/2013 to 19/july/2013 , is will throw it me with this error : The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
this is my code:
var format = "MM/dd/yyyy";
DateTime one = DateTime.ParseExact(startdate, format, CultureInfo.InvariantCulture);
DateTime two = DateTime.ParseExact(enddate, format, CultureInfo.InvariantCulture);
if (two >= one)
{
SqlConnection conn = new SqlConnection("Data Source=""catalog="";Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT Name,CLass, NRIC, StallNo, AmountSpent ,TimeDate=convert(nvarchar,timedate,103) FROM StudentTransactions WHERE TimeDate BETWEEN '" + one + "' AND '" + two + "'", conn);
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataSourceID = null;
GridView1.Visible = true;
GridView1.DataBind();
conn.Close();
}
19/7/2013 cannot be parsed using MM/dd/yyyy format as 19 is not a valid month.
You may want to use dd/MM/yyyy instead.
Date time format on local and Sever is different many times when you want to show date format in lables or texboxs and Add update in database . After a long fatigue I got this solution:
First check the current date format like below:
lblMsg.Text = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
if (lblMsg.Text == "dd/MM/yyyy")
txtd.Text = DateTime.Parse(dr["EventDate"].ToString()).ToString("dd/MM/yyyy");
else
txtd.Text = DateTime.Parse(dr["EventDate"].ToString()).ToString("MM/dd/yyyy");
Add or Update: Similarly you can check either the current date format is dd/MM/yyyy OR "MM/dd/yyyy"
if (lblMsg.Text == "dd/MM/yyyy")
usinfo.BDate = DateTime.ParseExact(txtDOB.Text.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
else
usinfo.BDate = DateTime.ParseExact(txtDOB.Text.ToString(), "MM/dd/yyyy", CultureInfo.InvariantCulture);
lblMsg.Text = "";

Calculating % of days VB.NET

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()

Convert vb.net to asp Classic

I have the following VB Code:
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)
If DateTime.Now < newdatetime Then
If value.DayOfWeek = DayOfWeek.Saturday Then
value = value.AddDays(2)
Return value
End If
If value.DayOfWeek = DayOfWeek.Sunday Then
value = value.AddDays(1)
Return value
End If
Return value
ElseIf DateTime.Now > newdatetime Then
Do
value = value.AddDays(1)
Loop While (value.DayOfWeek = DayOfWeek.Saturday) Or (value.DayOfWeek = DayOfWeek.Sunday)
Return value
End If
How would that be in ASP CLassic?
Can someone help ?
EDIT:
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)
Year: http://msdn.microsoft.com/en-us/library/fhzx965c%28v=vs.85%29.aspx
Month: http://msdn.microsoft.com/en-us/library/0eeeket2%28v=vs.85%29.aspx
Day of month: http://msdn.microsoft.com/en-us/library/yyhfe92k%28v=vs.85%29.aspx
Date comparison: http://msdn.microsoft.com/en-us/library/xhtyw595%28v=vs.85%29.aspx
Day of week: http://msdn.microsoft.com/en-us/library/t51x9wtx%28v=vs.85%29.aspx
Incrementing date: http://msdn.microsoft.com/en-us/library/cb7z8yf9%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/d1wf56tt%28v=VS.85%29.aspx is a fantastic reference for VBScript/ASP classic.

Specific Date and Time in ASP Classic

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 +

Resources