Solved

javascript date setTime from hh:mm:ss

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (http://www.cybersecurityventures.com/cybersecurity-market-report), worldwide spending on cybersecurity …
The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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