Solved

javascript date setTime from hh:mm:ss

Posted on 2009-03-30
3
1,157 Views
Last Modified: 2012-05-06
hi all i have a javascript date in a strucutre and i have a string of the time in hh:mm:ss format is it possible to set the dates time using this?

many thanks in advance.

Matt.
0
Comment
Question by:flynny
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 24019770
When you say that you "have a javascript date", do you mean that your
structure contains a javascript date object?  If so, it is very easy to do what
it is you are asking.

- Method #1
  Use the DateObj methods to set the Hour, Minute, and Second attributes:

  Given the time string in some variable, e.g., myTime = '01:23:45'

  var timeFields   = myTime.split( ':' )
  var myDateObj = new Date()
  myDateObj.setHour( parseInt( timeFields[ 0 ], 10 ) )
  myDateObj.setMinutes( parseInt( timeFields[ 1 ], 10 ) )
  myDateObj.setSeconds( parseInt( timeFields[ 2 ], 10 ) )

- Method #2
  Let the Date object handle things for you;

  Given some date object (e.g., "myDateOjb")

  var myDateObj = new Date()

  We can format the date portion as a string:

  function DateAsStr( when ) {
    function D2( val ) {
      return ( val < 10 ) ? '0' + val : val
    }
    return D2( when.getMonth() + 1 ) + '/' + D2( when.getDate() ) + '/' + when.getFullYear()
  }

  var myDateStr = DateAsStr( myDateObj )

  Then, we can append the timestamp portion to this string, and let the
  Date Object constructor handle it.

  var newDateObj( DateAsStr( myDateObj ) + ' ' + myTimeStr )

0
 
LVL 4

Expert Comment

by:jwmcpeak
ID: 24019801
Yep.
var time = "01:22:54";
 
var date = new Date();
 
var strs = time.split(":");
 
date.setHours(strs[0]);
date.setMinutes(strs[1]);
date.setSeconds(strs[2]);
 
alert(date);

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24027527
Thanks for the grade & points.

Good luck & have a great day
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to the codes 3 90
GridMvc Error mvc5 5 232
Why does my mouseover event only fire on the first item in my grid? 10 90
Javascript -- "RIGHT" function ? 3 198
A hard and fast method for reducing Active Directory Administrators members.
Not everyone has adapted to a rapid advancement in technology; there are people who are reluctant or afraid to delve into this brave new world of IT. If you have a friend or a family member who suffers from the so-called technophobia, here is how yo…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question