Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 24th, 2004, 08:57 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default Concatenate numbers

Does anyone know how to concatenate numbers? I am using the date() object and then trying to use the getMonth, etc. and put those together to form data as follows: 042320041200, which would be for April 4, 2004 at 12 noon. Has anyone ever done this?

Clay Hess
__________________
Clay Hess
 
Old April 25th, 2004, 06:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can use the Get functions to get the elements of the date you need and then use .toString to convert them to a string :

<html>
<body>
<script language="javascript">
var startDate = new Date();
var theDay = startDate.getUTCDate();
// as the month array starts at 0
var theMonth = startDate.getMonth() + 1;
var theYear = startDate.getFullYear();
var theHours = startDate.getHours();
var theMins = startDate.getMinutes();

// pad them out - convert 1 to 01 for formatting
if (theDay.toString().length==1)
{
    theDay = "0" + theDay;
}
if (theMonth.toString().length==1)
{
    theMonth = "0" + theMonth;
}
if (theYear.toString().length==1)
{
    theYear = "0" + theYear;
}
if (theHours.toString().length==1)
{
    theHours = "0" + theHours;
}
if (theMins.toString().length==1)
{
    theMins = "0" + theMins;
}

document.write (theMonth + theDay + theYear + theHours + theMins);
</script>
</body>
</html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate numbers and string mushu SQL Language 5 September 14th, 2004 09:20 AM
string concatenate phelkuizon Classic ASP Databases 1 September 13th, 2004 04:08 AM
concatenate with characters lily611 ADO.NET 1 July 21st, 2004 08:10 AM
How to Concatenate Data mbbolz SQL Language 4 April 1st, 2004 04:31 AM
Concatenate 2 variables into one? cpopham Access VBA 2 December 9th, 2003 10:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.