Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DateTime to String Issue

I feel like this is something I've done a thousand times so not sure why it is being so difficult now. I've created a method that simply returns Today's date for the user based on their UTC offset. But instead of returning a string resembling a date, it is returning this garbage

"䙭/䙭/Ἰ뻱䙭"

Here is the code.

public string getToday(Context context)
{
    var settings = PreferenceManager.GetDefaultSharedPreferences(context);
    var offset = settings.GetInt("offset", -5);
    var now = DateTime.UtcNow.AddHours(offset);

    return now.ToShortDateString();
}

When I step into the code using a breakpoint, offset and now both seem correct. now contains valid date parts all appearing to be accurate. Something about converting now to a string seems to go horribly wrong. Also tried:

return now.ToString("MM/dd/yyyy");

Same result. Weird part is the below code in another activity works without issue

var offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours;
var now = DateTime.UtcNow.AddHours(offset);
now.ToString("MM-dd-yyyy")
like image 905
jmease Avatar asked Oct 08 '22 17:10

jmease


1 Answers

Sounds to me like a localization issue. Make sure you're actually in English, be it en-US or similar.

like image 161
Zenexer Avatar answered Oct 13 '22 10:10

Zenexer