Solved

javascript date setTime from hh:mm:ss

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now