0

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? :)

1
  • a is a string so either you need to pull out the individual values to supply them as paramters to new Date(a[0], a[1] etc) or make the whole thing into a string ie. value = "Date(2014, 03, 09)" Commented Mar 5, 2014 at 10:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.