'Save Proto Timestamp as a string instead of object in Typescript
I currently have a birthday field in my proto definition defined as
google.protobuf.Timestamp birthday = 1;
When data is saved in the database its saved as an object of nanos and seconds which is normal
"birthday":{
"seconds":"563587200",
"nanos":0
}
But this date is not easily readable looking at the birthday field in the database.
Is there a way i can still use the google.protobuf.Timestamp
for the birthday definition and save it in a more readable format - say an ISOString format or so. e.g 1945-11-11T00:00:00.000Z
instead of an object of nanos and seconds - Something more readable
I tried this
const nanos: any = protoBirthday.nanos;
const seconds: any = protoBirthday.seconds;
const birthday: google.protobuf.Timestamp = new Date((seconds * 1000) + (nanos / 1000000)) as google.protobuf.Timestamp
where protoBirthday
is the birthday in protoTimestamp
This prints out the birthday in string format but when its saved its saved as an empty object
"birthday":{
}
i want something like this, if its possible
"birthday": "1945-11-11T00:00:00.000Z"
but still using the google.protobuf.Timestamp
type or if there is another way i can save it in a more readable format
Solution 1:[1]
This is very easy. For google.protobuf.Timestamp
, it will convert the Datetime to seconds and nanos to reduce the transportation size.
If you want to send the ISOString JSON is using, you can just use string as the type.
change google.protobuf.Timestamp birthday = 1;
to string birthday = 1;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Zichzheng |