How To Use Datetime Method On Sharpdevelop #4

in #utopian-io7 years ago (edited)

What Will I Learn?

Hello,in this tutorial i am about to show you guys how to calculate any time period by using datetime variable.

  • You will learn using datetime in c#
  • You will learn basic coding of sharpdevelop
  • You will learn c#

Requirements

  • Sharpdevelop
  • Little knowledge of c# coding
  • Little knowledge of using sharpdevelop

Difficulty

  • Basic

Tutorial Contents

Hello,course is continues...In this tutorial you will learn how to use "datetime" variable.Why is this important? It is important because you can use this variable to calculate every time period.This program will ask user their birth date,then it will calculate the number of days that user lived.

First of all we define our library as usual.

using System; using System.Collections.Generic; using System.Text;

Then we will define our integers which are year,month and day.

int year, month, day;

After defining our integers,we write our title for visuality.

Console.WriteLine("How many days you have lived for until today...\n--------------");

5.png

Then we ask users birth date in year,month and day to calculate.Then convert each our integers to 32 byte.

Console.Write("year : "); year = Convert.ToInt32(Console.ReadLine()); Console.Write("month :"); month = Convert.ToInt32(Console.ReadLine()); Console.Write("day :"); day = Convert.ToInt32(Console.ReadLine());

1.png

Then we will build a class for users input and equalize it to new class.This is where we use datetime method."datetime" will take todays date from database.

DateTime Birthdate = new DateTime(year, month, day);

This part is important.Every four years theres a day adding this line is to find out how many four years has passed.

int extraday = (DateTime.Today.Year - Birthdate.Year) / 4;

2.png

Now in this part first of all we asked users birthdate by year,month and day.First we multiply users year of birth to 365.Here comes another variable which is very useful; "Dayofyear" this feature is used for to calculate users year of birth year to this year.In the final step we sum up result to the extaday and we get the result.Finally we tell how many day that user has lived until today.

int result = (DateTime.Today.Year - Birthdate.Year) * 365 + (DateTime.Today.DayOfYear - Birthdate.Year) + extraday; Console.WriteLine(result + " day you have lived until today... "); Console.Read();

3.png

Output of the program;

4.png

Curriculum

If you want to start from somewhere in c# coding keep up with me.More to come...

Tutorials from first to last in order.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

Just a tip: you can use ``` above and below code snippets to create an entire code block (useful for when you have multiple lines).

Console.Write("year : ");
year = Convert.ToInt32(Console.ReadLine());

Also, for your next tutorial please improve your formatting and stop making everything bold.

You can contact us on Discord.
[utopian-moderator]

Thank you very much,i will use it on my next tutorials for sure

Hey @amosbastian, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

The contribution should not have been approved because the project has not been actually updated within the last year. There is only a commit fixing the headers in the readme file but no update with the code.

You can contact us on Discord.
[utopian-moderator]

Unvoted for breaking the Utopian Rules https://utopian.io/rules