Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how get month from a string in javascript

i have a date in string like this: var myDateStr='1431451872338.00';

i want, getMonth() from this format date, i do:var date = new Date(myDateStr); but always return invalid date.

and the method getMont() always return NaN, if I put this: var date = new Date(1431451872338.00); this return the date correct but with my string not

my var myDateStr get the value from json and is variable, if someone can help me thank you very much in advance, i hope do understand

like image 279
Diego Rivas C PROV Avatar asked Apr 09 '26 09:04

Diego Rivas C PROV


2 Answers

This works fine for me. You just need to be sure you're inputing a number, not a string.

var number = parseInt("1431451872338.00");
var date = new Date(number); //Tue May 12 2015 12:31:12 GMT-0500 (CDT)
var month = date.getMonth(); // 4
like image 157
Aweary Avatar answered Apr 11 '26 23:04

Aweary


A Date object cannot be instantiated with a string. You better 1st transform your string into an Int and then ask for month:

var myDateStr='1431451872338.00';
var date = new Date(parseInt(myDateStr, 10));
alert(date.getMonth());
like image 37
Gabriel Ciubotaru Avatar answered Apr 11 '26 23:04

Gabriel Ciubotaru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!