locked
how can I assign the datetime value into string variable in model class RRS feed

  • Question

  • User-1355965324 posted

    I have  the following model .  How can I store the datetime value which are stored in the column MOTDate , TaxDate  into the column MOTDateString, TaxDateString  automatically.

    public class Vehicles
        {
         
            public DateTime? MOTDate { get; set; }
            public DateTime? TaxDate { get; set; }
            public DateTime? RegDate { get; set; }       
            public DateTime? InsuredDate { get; set; }
            
            [NotMapped]
            public string MOTDateString {get;set;}
            [NotMapped]
            public string TaxDateString { get; set; }
    
            }        
           
    
     

    Wednesday, June 17, 2020 8:11 AM

Answers

  • User475983607 posted

    Pretty simple but confusing why you ask this question over and over.

        public class Vehicles
        {
            public DateTime? MOTDate { get; set; }
            public DateTime? TaxDate { get; set; }
            public DateTime? RegDate { get; set; }
            public DateTime? InsuredDate { get; set; }
            [NotMapped]
            public string MOTDateString 
            { 
                get 
                { 
                    return MOTDate?.ToString(); 
                } 
            }
            [NotMapped]
            public string TaxDateString { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 17, 2020 11:00 AM
  • User711641945 posted

    Hi polachan,

    Is there anything wrong?As mgebhard said,it could set the MOTDateString, TaxDateString automatically which depends on the value of  MOTDate , TaxDate.

    Model:

    public class Vehicles
    {
        public DateTime? MOTDate { get; set; }
        public DateTime? TaxDate { get; set; }
        public DateTime? RegDate { get; set; }
        public DateTime? InsuredDate { get; set; }
        [NotMapped]
        public string MOTDateString
        {
            get
            {
                return MOTDate?.ToString();
            }
        }
        [NotMapped]
        public string TaxDateString
        {
            get
            {
                return TaxDate?.ToString();
            }
        }
    }

    Controller(For easy testing,I just set the value manually):

    var data = new Vehicles()
    {
        MOTDate = DateTime.Parse("2019-8-7"),
        TaxDate = DateTime.Parse("2020-7-7")
    };

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 19, 2020 9:51 AM

All replies

  • User475983607 posted

    Pretty simple but confusing why you ask this question over and over.

        public class Vehicles
        {
            public DateTime? MOTDate { get; set; }
            public DateTime? TaxDate { get; set; }
            public DateTime? RegDate { get; set; }
            public DateTime? InsuredDate { get; set; }
            [NotMapped]
            public string MOTDateString 
            { 
                get 
                { 
                    return MOTDate?.ToString(); 
                } 
            }
            [NotMapped]
            public string TaxDateString { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 17, 2020 11:00 AM
  • User711641945 posted

    Hi polachan,

    Is there anything wrong?As mgebhard said,it could set the MOTDateString, TaxDateString automatically which depends on the value of  MOTDate , TaxDate.

    Model:

    public class Vehicles
    {
        public DateTime? MOTDate { get; set; }
        public DateTime? TaxDate { get; set; }
        public DateTime? RegDate { get; set; }
        public DateTime? InsuredDate { get; set; }
        [NotMapped]
        public string MOTDateString
        {
            get
            {
                return MOTDate?.ToString();
            }
        }
        [NotMapped]
        public string TaxDateString
        {
            get
            {
                return TaxDate?.ToString();
            }
        }
    }

    Controller(For easy testing,I just set the value manually):

    var data = new Vehicles()
    {
        MOTDate = DateTime.Parse("2019-8-7"),
        TaxDate = DateTime.Parse("2020-7-7")
    };

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 19, 2020 9:51 AM