How to get the day name from a selected date?
Asked Answered
J

14

74

I have this : Datetime.Now(); or 23/10/2009
I want this : Friday

For local date-time (GMT-5) and using Gregorian calendar.

Jordison answered 23/10, 2009 at 19:16 Comment(2)
after one of the exact same answers here is accepted ...do others clean up? :)Nixon
6 exact identical answers.. basically XDAhearn
T
145
//default locale
System.DateTime.Now.DayOfWeek.ToString();
//localized version
System.DateTime.Now.ToString("dddd");

To make the answer more complete:

Thighbone answered 23/10, 2009 at 19:18 Comment(2)
I wouldn't use the ToString. If you need it for comparisons or storage, just use the enum value. Otherwise, using the dddd format specifier as in Fredrik's answer is better for localization.Opposable
The localized version is not correct and gives a compiler error. It should be System.DateTime.Now.ToString("dddd");Egoism
K
17

If you want to know the day of the week for your code to do something with it, DateTime.Now.DayOfWeek will do the job.

If you want to display the day of week to the user, DateTime.Now.ToString("dddd") will give you the localized day name, according to the current culture (MSDN info on the "dddd" format string).

Kenney answered 23/10, 2009 at 19:25 Comment(0)
A
11
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.GetDayName(System.DateTime.Now.DayOfWeek)

or

System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.GetDayName(DateTime.Parse("23/10/2009").DayOfWeek)
Amerson answered 31/12, 2014 at 13:47 Comment(0)
A
8

DateTime.Now.DayOfWeek quite easy to guess actually.

for any given date:

   DateTime dt = //....
   DayOfWeek dow = dt.DayOfWeek; //enum
   string str = dow.ToString(); //string
Ahearn answered 23/10, 2009 at 19:19 Comment(1)
or to keep more simple Datetime dt; string date = dt.DayOfWeek.ToString()Polacre
P
8

Here is more simple

DateTime dt;
string yourdate = dt.DayOfWeek.ToString()

better than declare redundant DayOfWeek

Polacre answered 2/5, 2021 at 15:28 Comment(0)
B
6
DateTime now = DateTime.Now
string s = now.DayOfWeek.ToString();
Bookrack answered 23/10, 2009 at 19:19 Comment(0)
D
5

try this:

DateTime.Now.DayOfWeek
Diffidence answered 23/10, 2009 at 20:50 Comment(0)
I
2

You're looking for the DayOfWeek property.

Here's the msdn article.

Insoluble answered 23/10, 2009 at 19:19 Comment(0)
B
1

I use this Extension Method:

public static string GetDayName(this DateTime date)
{
    string _ret = string.Empty; //Only for .NET Framework 4++
    var culture = new System.Globalization.CultureInfo("es-419"); //<- 'es-419' = Spanish (Latin America), 'en-US' = English (United States)
    _ret = culture.DateTimeFormat.GetDayName(date.DayOfWeek); //<- Get the Name     
    _ret = culture.TextInfo.ToTitleCase(_ret.ToLower()); //<- Convert to Capital title
    return _ret;
}
Barkeeper answered 12/7, 2019 at 19:36 Comment(1)
Why this isn't the answer, I'll never knowWithrow
R
0

What about if we use String.Format here

DateTime today = DateTime.Today;
String.Format("{0:dd-MM}, {1:dddd}", today, today) //In dd-MM format
String.Format("{0:MM-dd}, {1:dddd}", today, today) //In MM-dd format
Rowland answered 28/6, 2015 at 3:56 Comment(0)
C
0
(DateTime.Parse((Eval("date").ToString()))).DayOfWeek.ToString()

at the place of Eval("date"),you can use any date...get name of day

Constitutional answered 17/1, 2018 at 10:51 Comment(0)
D
0
        Random Rnd = new Random();
        RandomDates Rdate = new RandomDates();
        PaymentDetails Payd = new PaymentDetails();
        DayOfWeek strDay = DateTime.Today.DayOfWeek;
        var dateTime = DateTime.Now;
        var dateValue2 = dateTime.ToString(@"MM\/dd\/yyyy");


        StepDescription = "Fill MatterInformation. ";

        
       

            Console.Write(" Input the Day : ");
            dt = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Input the Month : ");
            mn = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Input the Year : ");
            yr = Convert.ToInt32(Console.ReadLine());
            DateTime d = new DateTime(2021, 04, yr);
            var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            var diff = d.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
            if (diff < 0)
                diff += 7;
            var x = d.AddDays(-diff).Date;


            dateTime = DateTime.Now;
            dateValue2 = dateTime.ToString(@"MM\/dd\/yyyy");
            Console.WriteLine($"Date Value: {dateValue2}");

           // strDay = 

        }
        if (!strDay.Equals("Sunday") | !strDay.Equals("Saturday"))
        {
            Console.WriteLine("___________________OK____________________________________________");
            // string NotificateionDate = Rdate.DateWithin_PastDays(Rnd.Next(30, 260)).ToString(@"MM\/dd\/yyyy");

            // CustomLibrary.seWaitUntilElementIsVisible(10, NotiFiedDateTab.Actions.seGetLocator(), "NotiFiedDateTab");
            NotiFiedDateTab.Actions.Set(ControlPropertyNames.Text, dateValue2);

        }
        else
        {
            Console.WriteLine("_________________________NOT______________________________________");
            if (strDay.Equals("Sunday"))
            {
                dateTime = dateTime.AddDays(-2);
                dateValue2 = dateTime.ToString(@"MM\/dd\/yyyy");
                NotiFiedDateTab.Actions.Set(ControlPropertyNames.Text, dateValue2);
            }
            else if (strDay.Equals("Saturday"))
            {

                dateTime = dateTime.AddDays(-1);
                dateValue2 = dateTime.ToString(@"MM\/dd\/yyyy");
                NotiFiedDateTab.Actions.Set(ControlPropertyNames.Text, dateValue2);
            }
        }
      
    }
Devin answered 1/5, 2021 at 20:49 Comment(1)
Please provide some insightful commentary on what this code is doing for answer completeness.Revelry
T
0
using System;

class Program
{
    static void Main()
    {
        DateTime now = DateTime.Now;

        // Format today's date as "yyyy/MM/dd"
        var today = now.ToString("yyyy/MM/dd");

        // Get the current day as a string
        var day = now.Day.ToString();

        Console.WriteLine("Today's date: " + today);
        Console.WriteLine("Current day: " + day);
    }
}
Tribulation answered 15/7 at 13:59 Comment(0)
S
-2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessTheDay
{
class Program
{
    static void Main(string[] args)
    { 
 Console.WriteLine("Enter the Day Number ");
 int day = int.Parse(Console.ReadLine());
 Console.WriteLine(" Enter The Month");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Year ");
int year = int.Parse(Console.ReadLine());
DateTime mydate = new DateTime(year,month,day);
string formatteddate = string.Format("{0:dddd}", mydate);
Console.WriteLine("The day should be " + formatteddate);
}  
 } 
   }
Subsist answered 25/9, 2019 at 9:1 Comment(1)
Answer is not concise enough. You should read the question properly and give small working example that is easy to read & understand.Typebar

© 2022 - 2024 — McMap. All rights reserved.