0

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.

1
  • 2
    This is not react-native question. removing tag.Adding Javascript tag. Commented Feb 1, 2019 at 11:02

2 Answers 2

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

2

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();

4 Comments

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
You just need to import moment. Which you can do like import moment from 'moment' providing you're using npm/yarn for your packages. Otherwise you can import it from the CDN.
@VinayNarayankutty check this home page. momentjs.com/docs/.They have given in details how to use
@RIYAJKHAN your time doesn't work, the method you need is getSeconds() not getMilliseconds() I have edited your answer, if you could peer review it that'd be cool.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.