user3382908
user3382908

Reputation: 3

jQuery - creating date from input value

I really need your help to finish this

I'm working on my own script which import date from input which look like this:

<input type='text' class='hidden' value='$a' id='test'>

$a is date, taken from db with template(?): 2014, 03, 09, 04, 07, 02

And its: Year, Month, Day, Hour, Minute, Second

So it looks like this

<input type='text' class='hidden' value='2014, 03, 09, 04, 07, 02' id='test'>

My javascript file is like this:

$(function() {
    $('input#test').removeClass('hidden');
    var a = $('input#test').val();
    console.log(a);
    var b = +new Date(a);
    console.log(b);

The 1st console log returns what input has (2014, 03, 09, 04, 07, 02), but the 2nd one returns NaN. I have no idea why :c

When I write it manually like this:

var b = +new Date(2014, 03, 09, 04, 07, 02);

Its all working.. I have no idea how to solve it, I've also tried using this:

  1. span and .html();
  2. span and .text();

Any ideas? :)

Upvotes: 0

Views: 91

Answers (2)

Awlad Liton
Awlad Liton

Reputation: 9351

your a treated as string so it takes as first parameters of Date. You need to clarify other parameters in the Date. Why you are used + in the new Date() ?

you have to take care one more thing month is started from 0 so 03 means April not March.

It seems that it is not needed.

try this:

 $('input#test').removeClass('hidden');
    var a = $('input#test').val();
    console.log(a);
    arr = a.split(",")
    var b = new Date(arr[0],arr[1],arr[2],arr[3],arr[4],arr[5]);
    console.log(b);

working demo

Upvotes: 1

Mohsen ZareZardeyni
Mohsen ZareZardeyni

Reputation: 946

You are passing only one parameter (1 String) to the function! not 6!

use this:

$(function() { $('input#test').removeClass('hidden');

var a = $('input#test').val();

console.log(a);

var params = a.split(", ")

var b = +new Date(params[0], params[1], params[2], params[3], params[4], params[5]);

console.log(b);

Upvotes: 1

Related Questions