I want to split the datetime in the below object to date and time separately.
let A = {
datetime: '2019-02-01 15:30:43'
}
And save it in two different variables.
-
2This is not react-native question. removing tag.Adding Javascript tag.RIYAJ KHAN– RIYAJ KHAN2019年02月01日 11:02:12 +00:00Commented Feb 1, 2019 at 11:02
2 Answers 2
You can use split() e.g.
var splitString = A.datetime.split(" ");
// Returns array, ['2019-02-01','15:30:43']
You can access this with splitString[0] for the date and splitString[1] for the time.
Sign up to request clarification or add additional context in comments.
Comments
You can use moment.js library if you don't want any surprises.
moment(A.datetime).format('L'); // get date only
moment(A.datetime).format('LTS') // get time only.
You can have specific date and time format as well when using momentjs.
You may also use JavaScript's Date class for this.
const date = new Date(A.datetime);
//to get date
date.getDate() +' '+date.getMonth()+' '+date.getFullYear();
//to get time.
date.getHours()+' '+date.getMinutes()+' '+date.getSeconds();
answered Feb 1, 2019 at 11:07
RIYAJ KHAN
15.3k5 gold badges34 silver badges56 bronze badges
4 Comments
Vinay N
Do I need to import anything else to get "moment().format()" to working ? I'm using this in a react-native project, that's why I'm asking
RIYAJ KHAN
@VinayNarayankutty check this home page. momentjs.com/docs/.They have given in details how to use
lang-js