Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
myObj = function MyObject(r) {
        this.Prop = r.Prop
        this.LogIn = new Date(r.LogIn)
        this.Created_Date = new Date(r.Created_Date)
    }

How do I assign empty value "" if r.LogIn is null otherwise Date.

Right now if LogIn is null, its assigning it to date 12-31-1969. 


What I have tried:

myObj = function MyObject(r) {
        this.Prop = r.Prop
        this.LogIn = function(r){
		       if(r.LogIn ==null){
		           return "";
		       }
		     else{
		      return new Date(r.LogIn);
		     }
        this.Created_Date = new Date(r.Created_Date)
    }
Posted
Updated 1-Sep-17 8:11am
v2
Comments
Richard Deeming 1-Sep-17 13:55pm    
That doesn't look like any C# I've ever seen!
[no name] 1-Sep-17 14:06pm    
wrong tag.. its java script

1 solution

Try this:
JavaScript
this.LogIn = r.LogIn ? new Date(r.LogIn) : "";

Conditional (ternary) Operator - JavaScript | MDN[^]
 
Share this answer
 
Comments
[no name] 1-Sep-17 14:13pm    
thank you for coming out and helping me. I fix it.. thank you very much sir.
[no name] 1-Sep-17 17:23pm    
I liked this better.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900