Solved

javascript date setTime from hh:mm:ss

Posted on 2009-03-30
3
1,165 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Gain an elementary understanding of Blockchain technology.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

724 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