﻿ C# - First day of the current year and day of a date

# C# Sharp Exercises: Get the first day of the current year and first of a year against a given date

## C# Sharp DateTime : Exercise-46 with Solution

Write a program in C# Sharp to get the first day of the current year and first of a year against a given date.

C# Sharp Code:

``````using System;

class dttimeex46
{
static void Main()
{
int dt,mn,yr;
Console.Write("\n\n Find  the first day of a year against a date :\n");
Console.Write("---------------------------------------------------\n");
Console.WriteLine(" The First day of the current year is : {0} \n", FirstDayOfYear().ToString("dd/MM/yyyy"));
Console.Write(" Input the Day : ");
Console.Write(" Input the Month : ");
Console.Write(" Input the Year : ");
DateTime d = new DateTime(yr, mn, dt);
Console.WriteLine(" The formatted Date is : {0}",d.ToString("dd/MM/yyyy"));
DateTime nd=FirstDayOfYear(d);
Console.WriteLine(" The First day of the year {0} is : {1}\n", yr,nd.ToString("dd/MM/yyyy"));
}
static DateTime FirstDayOfYear()
{
return FirstDayOfYear(DateTime.Today);
}
static DateTime FirstDayOfYear(DateTime y)
{
return new DateTime(y.Year, 1, 1);
}
}
```
```

Sample Output:

``` Find  the first day of a year against a date :
---------------------------------------------------
The First day of the current year is : 01/01/2017

Input the Day : 12
Input the Month : 06
Input the Year : 2017
The formatted Date is : 12/06/2017
The First day of the year 2017 is : 01/01/2017
```

Flowchart :

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿