Is there a one-liner to get this value:
1536634800
Out of
Timestamp(seconds=1536634800, nanoseconds=0)
?
Is there a one-liner to get this value:
1536634800
Out of
Timestamp(seconds=1536634800, nanoseconds=0)
?
Use this regexp pattern:
console.log('Timestamp(seconds=1536634800, nanoseconds=0)'.match( /[0-9]{10}/g ));
let str = "Timestamp(seconds=1536634800, nanoseconds=0)".split(',')[0].split("Timestamp(seconds=").reverse()[0];
console.log(str);
To get the time inside of the string, you can do the following. Basically what is doing is using regex to match {10} numbers that are together.
TS
let time = 'Timestamp(seconds=1536634800, nanoseconds=0)'.match( /[0-9]{10}/g );
//Convert it into an actual date. Remeber to add a +1 to months since they start on zero 0.
let parsedTime = new Date(parseInt(this.time[0]));
//Store the formated date
let fomarmatedDate = this.formatDate(this.parseTime);
formatDate(time: Date) : String {
//In the mm we check if it's less than 9 because if it is your date will look like m/dd/yy
// so we do some ternary to check the number and get the mm
let mm = time.getMonth()+1<9 ? `0${time.getMonth()+1}` : time.getMonth()+1;
let dd = time.getDate();
let yyyy = time.getFullYear();
let date = `${mm}/${dd}/${yyyy}`;
return date
}
The result will be : 01/18/1970
You can make the code way shorter. I just did it this way so you can see how it works and what I'm doing.
To learn more about the .match take a look to this page https://www.w3schools.com/jsref/jsref_match.asp
You can use this tool to build your regex https://regexr.com/
function extractHrefValue(inputString: string): string | null {
const hrefRegex = /href\s*=\s*["']([^"']*)["']/i;
const match = inputString.match(hrefRegex);
if (match && match[1]) {
return match[1];
} else {
return null;
}
}