News:

Free Image Hosting:
http://image-host.developers-heaven.net
Share your images free!!!

Main Menu
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - admin

#301
C# Language / C# DateTime Structure
June 16, 2007, 07:50:24 AM
Here is an example of how the DateTime Structure works for C#. It's extremely basic and very easy to follow.

/*
This program finds out your Age using the DateTime structure in C#.
*/

using System;

class DOB{

private DateTime dtDob;
private DateTime dtNow;
private DateTime dtAge;
private int intDay;
private int intMonth;
private int intYear;
private int intHour;
private int intMinute;
private TimeSpan tsAge;
private int intAgeYear;
private int intAgeMonths;
private int intAgeDays;
private int intAgeHours;
private int intAgeMinutes;


public static void Main (String[] args) {

DOB objDob=new DOB();
objDob.getDob();
objDob.createDateObjects();

Console.WriteLine("Your Age in Years :" + objDob.getAgeInYears());
Console.WriteLine("Your Age in Months :" + objDob.getAgeInMonths());
Console.WriteLine("Your Age in Days :" + objDob.getAgeInDays());
Console.WriteLine("Your Age in Hours : " + objDob.getAgeInHours());
Console.WriteLine("Your Age in Minutes : " + objDob.getAgeInMinutes());
Console.WriteLine("Your Accurate Age is : " + objDob.getAgeInYears() + " Years " +
objDob.getMonthDiff() + " months " + objDob.getDayDiff() + " days");
}

/* get the date */

private void getDob() {

try {

Console.Write("Enter the Day u were born : " );
intDay=Console.ReadLine().ToInt32();
Console.Write("Month : ");
intMonth=Console.ReadLine().ToInt32();;
Console.Write("Year(yyyy) : ");
intYear=Console.ReadLine().ToInt32();
Console.Write("Hour(0-23) : ");
intHour=Console.ReadLine().ToInt32();
Console.Write("Minute(0-59) : ");
intMinute=Console.ReadLine().ToInt32();
}

catch (Exception e) {
Console.WriteLine(e.StackTrace);
Environment.Exit(0);
}
}

/* create the date objects */
private void createDateObjects() {

dtDob=new DateTime(intYear,intMonth,intDay,intHour,intMinute,0);
dtNow=DateTime.Now;

if (DateTime.Compare(dtNow,dtDob)==1)
tsAge=dtNow.Subtract(dtDob);
else {
Console.WriteLine("Future dates cannot be entered.");
Environment.Exit(0);
}

dtAge=new DateTime(tsAge.Ticks);
Console.WriteLine("Your date of birth :" + dtDob.Format("F",null));

}

/*  calculates the age in Years */
private int getAgeInYears() {
intAgeYear=dtAge.Year-1;
return intAgeYear;
}

/* calculates the age in months */
private int getAgeInMonths() {
intAgeMonths=intAgeYear*12;
intAgeMonths=intAgeMonths+(dtAge.Month-1);
return intAgeMonths;
}

/* calculates the age in days */
private int getAgeInDays() {
if (dtDob.Year==dtNow.Year) {
intAgeDays=dtNow.DayOfYear-dtDob.DayOfYear;
}
else {
if(DateTime.IsLeapYear(dtDob.Year))
intAgeDays=366-dtDob.DayOfYear;
else
intAgeDays=365-dtDob.DayOfYear;

for (int i=dtDob.Year+1;i < dtNow.Year;i++) {
if (DateTime.IsLeapYear(i))
intAgeDays+=366;
else
intAgeDays+=365;
}

intAgeDays+=dtNow.DayOfYear;
}
return intAgeDays;
}

/* calculates the age in Hours */
private int getAgeInHours() {
intAgeHours=getAgeInDays() * 24;
intAgeHours=intAgeHours+(dtNow.Hour-dtDob.Hour);
return intAgeHours;
}

/* calculates the age in Minutes */
private int getAgeInMinutes() {
intAgeMinutes=getAgeInHours() * 60;
intAgeMinutes=intAgeMinutes+(dtNow.Minute-dtDob.Minute);
return intAgeMinutes;
}

/* calculates the month part of the accurate Age */
private int getMonthDiff() {
return getAgeInMonths()%12;
}

/* calculates the day part of the accurate Age */
private int getDayDiff() {
int intDayTemp1=getAgeInDays();
int intDayTemp2;
int intTempYear;
int intTempMonth;
int intTempDay=intDay;

if (dtNow.Year!=dtDob.Year) {

if (1==dtNow.Month) {
intTempYear=dtNow.Year-1;
intTempMonth=12;
}
else {
if(dtNow.Day < intTempDay)
intTempMonth=dtNow.Month-1;
else {
intTempMonth=dtNow.Month;
intTempDay=1;
}
intTempYear=dtNow.Year;
}
}
else {
if (1==dtNow.Month || dtDob.Month==dtNow.Month)
return getAgeInDays();
else {
if(dtNow.Day < intTempDay)
intTempMonth=dtNow.Month-1;
else {
intTempMonth=dtNow.Month;
intTempDay=1;
}
}
intTempYear=intYear;
}

dtNow=new DateTime(intTempYear,intTempMonth,intDay);

intDayTemp2=getAgeInDays();

return intDayTemp1-intDayTemp2;
}
}


/*
Result
------
D:\progs>dob
Enter the Day u were born : 1
Month : 1
Year(yyyy) : 2000
Hour(0-23) : 1
Minute(0-59) : 4
Your date of birth :Saturday, January 01, 2000 1:04:00 AM
Your Age in Years :1
Your Age in Months :17
Your Age in Days :520
Your Age in Hours : 12489
Your Age in Minutes : 749375
Your Accurate Age is : 1 Years 5 months 3 days
*/

#302
Welcome to Developers Heaven Forum!

We hope you enjoy using our forum.  If you have any problems, please feel free to ask us for assistance.

Thanks!
~~27~
#303
Add a ref to the Microsoft Word 12.0 Object Library & import into the project
(using Microsoft.Office.Interop.Word;)


ApplicationClass wordApp = new ApplicationClass();

Document wordDoc = null;

string paramExportFilePath = @"C:\\Test.xps";

WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS;

bool paramOpenAfterExport = false;

WdExportOptimizeFor paramExportOptimizeFor =

WdExportOptimizeFor.wdExportOptimizeForPrint;

WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

int paramStartPage = 0;

int paramEndPage = 0;

WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

bool paramIncludeDocProps = true;

bool paramKeepIRM = true;

WdExportCreateBookmarks paramCreateBookmarks
= WdExportCreateBookmarks.wdExportCreateWordBookmarks;

bool paramDocStructureTags = true;

bool paramBitmapMissingFonts = true;

bool paramUseISO19005_1 = false;

try

{

// Open the source document.

wordDoc = wordApp.Documents.Open(

ref paramSourceDocPath, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing);

// Export it in the specified format.

if (wordDoc != null)

wordDoc.ExportAsFixedFormat(paramExportFilePath,

paramExportFormat, paramOpenAfterExport,

paramExportOptimizeFor, paramExportRange, paramStartPage,

paramEndPage, paramExportItem, paramIncludeDocProps,

paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

paramBitmapMissingFonts, paramUseISO19005_1,

ref paramMissing);

}

catch (Exception ex)

{

// Respond to the error

}

finally

{

// Close and release the Document object.

if (wordDoc != null)

{

wordDoc.Close(ref paramMissing, ref paramMissing,

ref paramMissing);

wordDoc = null;

}

// Quit Word and release the ApplicationClass object.

if (wordApp != null)

{

wordApp.Quit(ref paramMissing, ref paramMissing,

ref paramMissing);

wordApp = null;

}

GC.Collect();

GC.WaitForPendingFinalizers();

GC.Collect();

GC.WaitForPendingFinalizers();