Time interval
Jednostavan ali i neophodan u svakodnevnom radu mnogih ljudi , kako u nauci tako i u poslovnoj primeni , program ' Time interval ' je namenjen za izračunavanje vremenskog rastojanja između dva odabrana datuma. Po odabiru početnog i krajnjeg datuma , program izračunava koliko je sekundi proteklo između dva datuma i obijeni rezultat u sekundama preračunava u godine , mesece , dane , sate , minute i sekunde. Tako preračunat rezultat prikazuje korisniku u okviru istoimenih zasebnih ekstualnih polja. Način dobijanja rezultata pogledajte u metodi ' void CalculateTimeInterval() ' u okviru programskog koda ispod. Program je precizan za vremenski interval od ( 01.01.1753 00:00:00 ) do ( 31.12.9998 00:00:00 ). Pokušajte da uz pomoć programa izračunate koliko je tačno vremena prošlo od datuma vašeg rođenja do danas , ili razliku u godinama između dva istorijska događaja. Na dnu strane se nalaze linkovi za preuzimanje besplatnog programa i zip arhive sa kompletnim projektom za IDE SharpDevelop , C# (^) . Za besprekoran rad programa neophodan je i .Net framework 4.0 ( ^ ).
/**********************************************************************************************************
* Program name : Time interval *
* Program ver. : 1.0 *
* Created by : SharpDevelop *
* Code author : Perić Željko *
* Code language : C# *
* Date created : 12.12.2013 *
* Time created : 17:31 *
* *
* *
* Program Description : Program for calculating time distance between two dates. *
* *
* *
* Program Accuracy Notice : Program calculates distance between two dates by finding *
* how many seconds has passed from start date to the end date, *
* and then recalculates seconds into *
* years , months , days , hours , minutes and seconds. *
* *
* exmpl: *
* start date-time 29.03.1972 08:40:00 *
* end date-time 12.12.2013 17:34:58 *
* *
* result *
* 41 years *
* 8 monts *
* 14 days *
* 8 hours *
* 54 minutes *
* 58 seconds *
* *
* Minimum date value 01.01.1753 00:00:00 *
* Maximum date value 31.12.9998 00:00:00 *
* *
* All the best, *
* Author. *
**********************************************************************************************************/
using System;
using System.Windows.Forms;
namespace Time_interval
{
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// Set initial values
//
SetInitialValues();
}
void SetInitialValues()
{
//
// Set initial date and time
//
DateTime dt = new DateTime();
dt = DateTime.Now;
dateTimePicker1.Text = dt.ToString();
//
// Set min value for dateTimePicker2
//
dateTimePicker2.MinDate = dateTimePicker1.Value;
}
void DateTimePicker1CloseUp(object sender, EventArgs e)
{
//
// event happens when user selects date from control
// by opening , selecting and closing control
//
//
// Set min value for dateTimePicker2
//
dateTimePicker2.MinDate = dateTimePicker1.Value;
CalculateTimeInterval();
}
void DateTimePicker2CloseUp(object sender, EventArgs e)
{
//
// event happens when user selects date from control
// by opening , selecting and closing control
//
CalculateTimeInterval();
}
void DateTimePicker1ValueChanged(object sender, EventArgs e)
{
//
// event happens when user changes date
// by typing in new value for date
//
//
// Set min value for dateTimePicker2
//
dateTimePicker2.MinDate = dateTimePicker1.Value;
CalculateTimeInterval();
}
void DateTimePicker2ValueChanged(object sender, EventArgs e)
{
//
// event happens when user changes date from control
// by typing in new value for date
//
CalculateTimeInterval();
}
void CalculateTimeInterval()
{
//
// declaration of variables
//
DateTime StartDate;
DateTime EndDate;
TimeSpan TimeInterval;
long years = 0;
long months = 0;
long days = 0;
long hours = 0;
long minutes = 0;
long seconds = 0;
long reminder = 0;
long i = 0;
long j = 0;
StartDate = dateTimePicker1.Value;
EndDate = dateTimePicker2.Value;
if(EndDate.TimeOfDay<StartDate.TimeOfDay)
{
EndDate = EndDate.AddDays(-1);
}
TimeInterval = EndDate.Subtract(StartDate);
seconds = (long)TimeInterval.TotalSeconds;
minutes = Math.DivRem(seconds,60,out reminder);
seconds = reminder;
hours = Math.DivRem(minutes,60,out reminder);
minutes = reminder;
days = Math.DivRem(hours,24,out reminder);
hours = reminder;
years = EndDate.Year - StartDate.Year;
months = (12-StartDate.Month) + ((years-1)*12) + EndDate.Month;
years = Math.DivRem(months,12,out reminder);
months = reminder;
days = EndDate.Day-StartDate.Day;
if(days<0)
{
switch (StartDate.Month)
{
case 1:j=31;
break;
case 2:j=28;if(DateTime.IsLeapYear(StartDate.Year)){j++;}
break;
case 3:j=31;
break;
case 4:j=30;
break;
case 5:j=31;
break;
case 6:j=30;
break;
case 7:j=31;
break;
case 8:j=31;
break;
case 9:j=30;
break;
case 10:j=31;
break;
case 11:j=30;
break;
case 12:j=31;
break;
}
j-= StartDate.Day;
i = EndDate.Day;
days = i+j;
months--;
if(months<0)
{
years--;
months+=12;
}
}
textBox6.Text = seconds.ToString();
textBox5.Text = minutes.ToString();
textBox4.Text = hours.ToString();
textBox3.Text = days.ToString();
textBox2.Text = months.ToString();
textBox1.Text = years.ToString();
}
}
}
/************************************************************************
* Program Licence : *
* *
* Copyright 2012 , Perić Željko *
* (periczeljkosmederevo@yahoo.com) *
* *
* According to it's main purpose , this program is licenced *
* under the therms of 'Free Software' licence agreement. *
* *
* If You do not know what those therms applies to *
* please read explanation at the following link : *
* (http://www.gnu.org/philosophy/free-sw.html.en) *
* *
* Since it is Free Software this program has no warranty of any kind. *
************************************************************************
* Ethical Notice : *
* *
* It is not ethical to change program code signed by it's author *
* and then to redistribute it under the same name , especially *
* if it is incorrect. *
* *
* It is recommended that if you make improvement in program code , *
* to make remarks of it and then to sign it by Your's name , *
* for further redistribution as new major version of program. *
* *
* Author name and references of old program code version should be *
* kept , for tracking history of program development. *
* *
* For any further information please contact code author at his email. *
************************************************************************/
/************************************
* List Of Revisions *
************************************
* *
* *
* *
* *
* *
* *
* *
************************************/