Mayur Saner
Mayur Saner

Reputation: 462

How to get date type value from object?

I want to get all the values from an object whose data type is a date. How does that possible. And also after that, I want to convert it into UTC and save it.

e.g.
{
    employeeid: "4",
    id: 276,
    birthdate: "Thu Mar 26 2020 05:30:00 GMT+0530 (India Standard Time)",
    status: "pending"
}

From this object I just want birthdate(I won't be knowing the property name it can be anything). I want to make this code generic because the variable name varies and I want to use this for the whole application.

Upvotes: 0

Views: 1702

Answers (2)

Sajeeb Ahamed
Sajeeb Ahamed

Reputation: 6390

You can parse a string as a date. If the string is a valid date then it returns a number representing the milliseconds elapsed since January 1, 1970, 00:00:00 UTC, otherwise, it returns NaN.

/**
 * Check if the string is a valid date string or not.
 * For that we check if the given string is really a string, not a number.
 * Because a number is parsed as a date by Date.parse()
 */
function isDate(str) {
    let date = Date.parse(str);
    return (typeof str === 'object' && str instanceof Date) || (typeof str === 'string' && isNaN(+str) && !isNaN(date));
}

/**
 * These are some examples
 *
 */
let dateString = "Thu Mar 26 2020 05:30:00 GMT+0530 (India Standard Time)";
let nonDateString = "Hello world";
let object = {
    employeeid: "4",
    id: 276,
    birthdate: "Thu Mar 26 2020 05:30:00 GMT+0530 (India Standard Time)",
    status: "pending"
};


console.log('Date string: ', isDate(dateString));
console.log('Non date string:', isDate(nonDateString));

// Check a date object rather than a date string. It works for it as well.
console.log('Date object:', isDate(new Date()));

if (isDate(object.birthdate)) {
    console.log(`${object.birthdate}: is a date string.`);
} else {
    console.log(`${object.birthdate}: is not a date string.`);
}
.as-console-wrapper{min-height: 100%!important; top: 0;}

Update

For your changed query I am updating this answer. As you need all the values of the object with date strings so for that I am generating a new object with all the dates and converts the date strings to UTC string.

/**
* Check if a string is a date or not.
*/
function isDate(str) {
    let date = Date.parse(str);
    return (typeof str === 'object' && str instanceof Date) || (typeof str === 'string' && isNaN(+str) && !isNaN(date));
}


const obj = {
    employeeid: "4",
    id: 276,
    birthdate: "Thu Mar 26 2020 05:30:00 GMT+0530 (India Standard Time)",
    status: "pending",
	join: Date('2020-01-12')
};

/**
* This will makes a new object with the date types
* and also convert the date into a UTC string.
*/
const dates = Object.entries(obj).reduce((a, [key, value]) => {
    return isDate(value) ? {...a, [key]: new Date(value).toUTCString()} : a;
}, {});

console.log(dates);
.as-console-wrapper {min-height: 100%!important; top: 0;}

Upvotes: 2

Hien Nguyen
Hien Nguyen

Reputation: 18975

You can use instanceof and typeof to check data type.

I made an demo to check your data type

export class AppComponent {
  name = "Angular";
  mydata = {
    employeeid: "4",
    id: 276,
    birthdate: "Thu Mar 26 2020 05:30:00 GMT+0530 (India Standard Time)",
    status: "pending"
  };
  birthdayType: any;
  realType: any;

  constructor() {
    let dob: any = this.mydata.birthdate;
    this.birthdayType = dob instanceof Date;
    this.realType = typeof dob;
  }
}

export class Data {
  public employeeid: string;
  public id: number;
  public birthday: Date;
  public status: string;
}

Stackbliz: https://stackblitz.com/edit/angular-bw4b8d

Upvotes: 1

Related Questions