xamarin get the current date

RaniaMKRaniaMK Member ✭✭

how can i get the current date .i use Date = DateTime.Today.ToString("dd/mm/yy HH:mm"), and Date = DateTime.Now.ToString("dd/mm/yy HH:mm"),the result is 01/01/0001 00:00:00 ?

Tagged:

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    DateTime..Date would be the date.
    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netframework-4.8

    Why are you doing a ToString on it? Keep it as a DateTime object in your code. Always keep object as objects - as their correct type. Don't convert them to strings to work with internally.

    Use StringFormat in your XAML to adjust how it is displayed for the user - but keep it as a real object in your code.
    You don't want to force the formatting to the way you like it. What about people in... Germany? The formatting is different. Use globalization/localization of the device itself to present the time and date in a way the user accepts and expects. Otherwise the user sees it as just another American wanker trying to push their way on the world - and that doesn't help the way your software is received. So something like ToShortDate and ToShortTIme are preferable so t

    You should also look up the correct formatting coded
    "dd/mm/yy HH:mm" If mm is month then it can't also be minutes, right? Just guessing at this stuff rarely yeilds positive results.
    http://blog.stevex.net/string-formatting-in-csharp/

  • JohnHardmanJohnHardman GBUniversity mod
    edited May 29

    @RaniaMK said:
    how can i get the current date .i use Date = DateTime.Today.ToString("dd/mm/yy HH:mm"), and Date = DateTime.Now.ToString("dd/mm/yy HH:mm"),the result is 01/01/0001 00:00:00 ?

    This forum is for Xamarin.Forms related questions. For C# questions, please post on StackOverflow or another site instead.

    For DateTime formatting, see the documentation page at https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

    As @ClintStLaurent said above, your month specifier is wrong.

  • BobWhittenBobWhitten USMember ✭✭

    Agree with @JohnHardman -- this is more a .NET question. That said, hard to know why you're not getting the date/time. You didn't mention the platform you are on (iOS, Android, UWP) -- is it something other than that?

    I have successfully used the following code:

    public static string summaryDateTimeFmt = "yyyy/MM/dd HH:mm";
    

    ...
    summTimeStamp = DateTime.Now.ToString( summaryDateTimeFmt );

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited May 29

    @JohnHardman said:
    This forum is for Xamarin.Forms related questions. For C# questions, please post on StackOverflow or another site instead.

    While I totally agree - That fight is long gone. 90% of the questions over the last 12-18 months have been kitchen-table wackers that haven't so much as read "Learn C# in 30 days".

    I don't necessarily blame them. I blame Microsoft for their current line of advertising. They are pushing Xamarin as a technology that requires little or no understanding. They are pushing it on web developers that only know HTML and CSS as a way to make apps that run on devices. So its not exactly a rookie's fault that they read the hype and believed it.

    I really, really, really, really miss the days when Xamarin was a paid-for eco-system and therefore used by professionals or at least serious hobbyists that would do some research and take all the foundation education first so they were ready when their expensive timed license period started. Paying a premium price was a natural filter for the unprepared.

    Here's a thought...

    What if the Xamarin eco-system were time limited then expired without a license number, that you got from completing the MS Learn course? You give new adopters a real, bona fide reason and motivation to do the formal training. Its still all free. But you pretty much have to do the courses if you want to use the system more than 60 days.

  • jezhjezh Member, Xamarin Team Xamurai

    @RaniaMK
    You can use this:

    string  now=  DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss");
    

    The result string format is like this: 30/05/2019 14:01:33
    Note: because / means default date separator.
    Fore more details, you can check: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#dateSeparator

Sign In or Register to comment.