ZeroZek
ZeroZek

Reputation: 319

Bootstrap datetimepicker parsing data

I want to parse hours and minutes from selected date&time.

var datetime = $('#datetimepicker').data('date'); 

gives: 2017-11-01 11:50

If I want to parse hours and minutes :

time = datetime.getHours()+':'+datetime.getMinutes();

error will occure : Uncaught TypeError: datetime.getHours is not a function.

Which functions to use, to parse data from date above?

Upvotes: 0

Views: 301

Answers (3)

Svela
Svela

Reputation: 639

Use the Date object:

var datetime = new Date($('#datetimepicker').data('date'));
var time = datetime.getHours()+':'+datetime.getMinutes();

Upvotes: 1

Daniel Mihai Petrariu
Daniel Mihai Petrariu

Reputation: 11

Are you sure that your variable is date type. getHours() is a JS function for getting the hours from a date.
Double check the type before. If is not, then you just parse the datetime string, using
let minutes = parseInt(datetime.split(' ')[1].split(':')[0]);

Upvotes: 0

Kshitij Kumar
Kshitij Kumar

Reputation: 350

You can use split on input field data.

var time = datetime.split(" ")[1];
var hours = time.split(":")[0];
var minutes = time.split(":")[1];

Please let me know which library are you using so that I can give you exact solution.

Upvotes: 0

Related Questions