javascript convert json into timestamp splits

I have a json and it have a time stamp string
What I need to do is split this string into the hour , min and second componets
Also if the Activity_time has no value something like "Activity_Time":"", how do I add a default values to the time splits

      var json = {"CustomerNum":"12345","Activity_Date":"1/24/16","Activity_Time":"06:00:00"};

      var CustNum =  json.CustomerNum
      
      var datesplit = new Date(json.Activity_Date);
      var ActivityMonth = datesplit.getMonth() + 1;
      var ActivityDay = datesplit.getDate();
      var ActivityYear = datesplit.getFullYear();
      
      // the time is pulled from Activity_time, and has a format of HH24:MI:SS
      
      var ActivityHR   = // ActivityHR  pull from the 1st char up to the 1st :    -- default to 12 to be midday
      var ActivityMIN  = // ActivityMIN pull from the 1st : up to the second : -- default to 00
      var ActivitySEC  = // ActivitySEC pull from the second : up to the end . -- default to 00
LVL 2
Jayesh AcharyaTechnichal ConsultantAsked:
Who is Participating?
 
zephyr_hex (Megan)DeveloperCommented:
Create a date object using Activity_Date and Activity_Time, and then use getHours(), getMinutes() and getSeconds().

var json = {"CustomerNum":"12345","Activity_Date":"1/24/16","Activity_Time":"06:00:00"};

var d = new Date(json.Activity_Date + " " + json.Activity_Time);
console.log(d);
console.log(d.getHours());
console.log(d.getMinutes());
console.log(d.getSeconds());

Open in new window

1
 
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
thanks
0
 
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
in javascript,

I want to add a simple default

 to the json.Activity_Date
and json.Activity_Time

somthing like

var dstring = "";
var tstring = "";

if json.Activity_Date is "" then
      dstring = "01/01/3000";
else
       dstring = json.Activity_Date;
end if;

if json.Activity_Time is "" then
      tstring = "12:00:00";
else
       tstring = json.Activity_Time;
end if;

so I can then do the following line
var d = new Date(dstring + " " + tstring);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.