Morpheusweb.it - Risorse per webmaster: script, ASP.NET, C#, Visual Basic .Net, tutorial, asp, javascript, css, php, html, java, ADO, VBScript, forms, frames, Active Server Pages, Dynamic HTML, database, gratis per webmaster e webdesigner

Una classe DateDiff per calcolare l'intervallo tra due date


La classe DateDiff

Imports System
  Namespace myApps
    Public Class DateDiff
      Public Sub DateDiff()
        'costruttore di default
      End Sub
      Public Enum howToCompare
        m = 1
        s
        t
        mm
        yyyy
        q
        d
      End Enum
      Public Shared Function GetDiff(ByVal mode As howToCompare, ByVal startDate As System.DateTime, ByVal endDate As System.DateTime) As Double
        Dim diff As Double = 0
        Try
          Dim ts As System.TimeSpan = New System.TimeSpan(startDate.Ticks - endDate.Ticks)
          Select Case (mode.ToString().ToLower())
            Case "m"
              diff = Convert.ToDouble(ts.TotalMinutes)
              Exit Select
            Case "s"
              diff = Convert.ToDouble(ts.TotalSeconds)
              Exit Select
            Case "t"
              diff = Convert.ToDouble(ts.Ticks)
              Exit Select
            Case "mm"
              diff = Convert.ToDouble(ts.TotalMilliseconds)
              Exit Select
            Case "yyyy"
              diff = Convert.ToDouble(ts.TotalDays / 365)
              Exit Select
            Case "q"
              diff = Convert.ToDouble((ts.TotalDays / 365) / 4)
              Exit Select
            Case Else 'd
              diff = Convert.ToDouble(ts.TotalDays)
              Exit Select
          End Select
      Catch
        diff = -1
      End Try
      Return diff
    End Function
  End Class
End Namespace


Un esempio di utilizzo

<%@ Page language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<script runat="server">
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  Dim oggi As DateTime = DateTime.Now
  Dim domani As DateTime = oggi.AddDays(1)
  Dim diff As Double = _
  myApps.DateDiff.GetDiff(myApps.DateDiff.howToCompare.m, domani, oggi)
  Response.Write(diff.ToString())
End Sub
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>



Scarica il Codice...


Stampa la pagina


 


Inizio pagina

risorse per webmaster