Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to display the day of week in asp.net

i have date as follows

01/01/2015

for the above date i want to display the day of the week in asp.net

for that how can i do in asp.net using c#.

Regards,
Narasiman P.
Posted

C#
//Use the DateTime class
//The constructor is, new DateTime(int yyyy, int month,int day)
DateTime dateTime = new DateTime(2015, 1, 1);
//You can also parse a string
DateTime dateTimeFromString = DateTime.Parse("01/01/2015");
//To display the day as its full name e.g. Monday
string dayOfWeekLong=  dateTime.ToString("dddd");
//To display the day as a shortened name e.g. Mon
string dayOfWeekShort = dateTime.ToString("ddd");
//To display as numeric e.g. 09, 29
string dayOfWeekNumericLong = dateTime.ToString("dd");
 
Share this answer
 
Use below code.

int dayofweek = Convert.ToInt16(Convert.ToDateTime("01/01/2015").DayOfWeek);
 
Share this answer
 
Try this:
C#
using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string dateString = "01/01/2015";
        string format = "dd/MM/yyyy";
        DateTime aDate;
        if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,
            DateTimeStyles.None, out aDate))
        {
            Console.WriteLine("The day of the week for {0} is {1}.", dateString, aDate.DayOfWeek);
        }
    }
}

1. use DateTime.TryParseExact Method[^] to parse the date string to a pre-determined datetime format.
2. use DateTime.DayOfWeek Property[^] to return the day of the week for that date.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900