Solved

javascript date setTime from hh:mm:ss

Posted on 2009-03-30
3
1,123 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the grade & points.

Good luck & have a great day
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

7 Experts available now in Live!

Get 1:1 Help Now