简体   繁体   中英

Get full day Name from DateTime in C#

I have js calender when I select date from calender it calls C# service which takes string as parameter . It is getting date in format Mon Apr 18 2016 00:00:00 GMT 0500 . I want Monday intead of Mon.

My java Script code is

$scope.deliveryDateTime = {  Day: new Date(2015, 11, 28, 14, 57) };
 var DeliveryDay =  $scope.deliveryDateTimeData.Day;

        $http.get(meta.service.GetTimeAvlb+'?day='+ DeliveryDay).
           success(function (data) {
               debugger;
               console.log("Data" + data);
               $scope.AvailiableTimeData = data.data;                  
               }                 
           }).
           error(function (data) {
           }); 

Here is my C# Service code

public ApiResult GetTimeAvlb( string day )
     {                    
        var splitDay = day.Split(' ');
        string dayName1 = splitDay[0].ToString();            
        apiresilt.data = db.TimeAvaliblities.Where(ss => ss.DayName == dayName1).ToList();
        return apiresilt;
     }

here dayName1 returning Mon while my db has value Monday .

.ToString("Format-String") can be used for display date in required format;

DateTime dt = DateTime.Now; will be the current date APR/01/2016
string str = dt.ToString("dddd"); // this will be Friday for 04/01/2016

If you really wanna parse this string to DateTime , you have to use your GMT 0500 (Pakistan Standard Time) part as a string literal. DateTime parsing methods does not support timezone names and UTC Offset part without TimeSeparator .

Then you can use DateTime.ToString method with dddd specifier and english-based culture like InvariantCulture .

var s = "Mon Apr 18 2016 00:00:00 GMT 0500 (Pakistan Standard Time)";
var dt = DateTime.ParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT 0500 (Pakistan Standard Time)'",
                             CultureInfo.InvariantCulture);

Console.WriteLine(dt.ToString("dddd", CultureInfo.InvariantCulture));

By the way, there is no such a thing as date time string . Your data either can be DateTime or string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM