This code yet is not really optimize and I know that . But for now this is all I have.

What this code does is that it computes the expiration date of an account.

By the way I need to do the swapping workaround because the date that the server gives to me are inverted so I need to do a lil workaround about that.

    /// <summary>
    /// Add revalidation text and how many days the agents account will expire
    /// </summary>
    public async void RevalidateAccount()
        //Get Stored Persistent Data of Date
        string _expDateFromPersistent = await SecureStorage.GetAsync("ExpirationDate");

        //Check if empty or not
        //because if it is empty we need to hide the validation text first
        //then show it after relogging in
        if (!string.IsNullOrEmpty(_expDateFromPersistent))
            IsVisibleValidationText = true;
            //Convert string date to DateTime
            DateTime expDate = Convert.ToDateTime(_expDateFromPersistent);
            DateTime todayDate = DateTime.Now;
            //Change Format
            string _dateToday = todayDate.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);

            //Swap workaround
            string day = expDate.Day.ToString();
            string month = expDate.Month.ToString();
            string year = expDate.Year.ToString();
            string _expirationDate = year + "/" + day + "/" + month; 
            TimeSpan expirationDate = DateTime.Parse(_expirationDate) - DateTime.Parse(_dateToday);
            //Business Rule: 7 days prior to 30 days show the revalidation text
            if (expirationDate.Days <= 7)
                if (IsVisibleValidationText)
                    //Formatted Text for validation text
                    Revalidate = new FormattedString();
                    Revalidate.Spans.Add(new Span { Text = "You have ", ForegroundColor = Color.FromHex(_textColor), FontSize = 15 });
                    Revalidate.Spans.Add(new Span { Text = $"{expirationDate.Days} days", ForegroundColor = Color.FromHex(_textColor), FontSize = 15, FontAttributes = FontAttributes.Bold });
                    Revalidate.Spans.Add(new Span { Text = " left before you need \n to re-validate your login credentials.", ForegroundColor = Color.FromHex(_textColor), FontSize = 15 });
                    await _pageDialogService.DisplayAlertAsync("NOTICE", "You have " + expirationDate.Days + " days left before you need to re-validate your login credentials.", "OK");
            IsVisibleValidationText = false;

What my code does is that it gets the data(Expiration Date) from the server then I just need to differ the DateTime.Now and the date(expiration date) from the server to have an expiration date.

  • 2
    \$\begingroup\$ ok, I think the question is fixed now ;-) \$\endgroup\$
    – t3chb0t
    Commented Aug 30, 2019 at 7:26

1 Answer 1


General naming guidelines suggest that an async method should end its name with "Async", so the method name should be RevalidateAccountAsync.

Some would argue that a underscore prefix should never be used for naming of any variables. For those that do allow underscore prefixes, it should only be for class-level variables and never used for variables local to a given method.

When comparing or manipulating 2 DateTime objects, the proper way to do that is to be sure that both have the same Kind. Oddly enough, your code does that since you jump through so many hoops to create new DateTime objects with Kind of Unspecified.

expirationDate is a misleading name since it is not a date. It is a TimeSpan.

After checking expirationDate.Days <= 7, there is no need to check IsVisibleValidateText since it will always be true at that point.

Your code looks to be twice as long as it needs to be because of all the manipulation of DateTime and string. For example, to get today's date should only be 1 line:

DateTime todayDate = DateTime.Today; \\Kind is Local

expDate could use DateTime.Parse or Convert.ToDateTime. However, you should also set it's Kind because as it is it will be Unspecified unless the string contains any time zone offset. Example setting it to Local:

DateTime expDate = DateTime.SpecifyKind(Convert.ToDateTime(_expDateFromPersistent), DateTimeKind.Local);

The alternative would be to keep expDate as Unspecified but then make todayDate also Unspecified.

DateTime todayDate = DateTime.SpecifyKind(DateTime.Today, DateTimeKind.Unspecified);

Since it looks like you are interested in dates starting a midnight, skip the slower string manipulation and instead use the DateTime.Date property. See help at this link. Example:

TimeSpan expirationDate = expDate.Date - todayDate;

Which gets rid of all the slower and unneeded string variables, both dates above have the same Kind, and both are set to midnight.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.