Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

javascript formatting date format

I have a script that is used for adding or subtracting a number of days to some dates.

for example if I have a date that is 10/08/2012 And I do +5 it should return 15/08/2012

If we are the end of the month it should return the next month.

Actually this script seems to be good, but the thing is the date format. It returns something like a timestamp and I don't really know how to format it, because javascript is not so easy for me I've tried to find to do something like in php date('d-m-y', strtotime($row['date']) But I did not find the equivalent for javascript.

First of all here comes the code I used to have all dates.

<?php $jours=d ate( 'Y-m-d'); $sql="SELECT * FROM agenda WHERE n_doss='"
.mysql_real_escape_string($_GET[ 'n_doss']). "' AND qualite='".mysql_real_escape_string($_GET[
'qualite']). "' AND liasse='".$_GET[ 'liasse']. "'"; $qry=m ysql_query($sql) or
die(__LINE__.mysql_error().$sql); $i=- 1; // index des enregistrements
?>
 <form action="<?php echo (isset($_POST['go'])) ? 'go2.php' : '#'; ?>"
 method="post">
 <input type="hidden" name="liasse" value="<?php echo $_GET['liasse']; ?>"
 />
 <input type="hidden" name="n_doss" value="<?php echo $_GET['n_doss']; ?>"
 />
 <table id="box-table-a">
 <tr>
 <th scope="col">
 <input name="data[<?php echo ++$i; ?>][code_s]" type="text" value="CODE S"
 size="10">
 </th>
 <th scope="col">
 <input name="data[<?php echo $i; ?>][libelle]" type="text" value="LIBELLE"
 size="30">
 </th>
 <th scope="col">
 <input name="data[<?php echo $i; ?>][action]" type="text" value="ACTION"
 size="15">
 </th>
 <th scope="col">
 <input name="data[<?php echo $i; ?>][libelle]" type="text" value="DESCRIPTION"
 size="40">
 </th>
 <th scope="col">
 <input type="text" name="data[<?php echo $i; ?>][date]" value="DATE D'ACTION"
 size="12">
 </th>
 <th scope="col">
 <input type="text" name="data[<?php echo $i; ?>][date]" value="VALIDATION"
 size="12">
 </th>
 <th scope="col"><strong>ETAT</strong>
 </th>
 </tr>
 <?php while($row=m ysql_fetch_assoc($qry)): ?>
 <tr>
 <td>
 <input name="data[<?php echo ++$i; ?>][code_s]" type="text" value="<?php echo $row['code_s'];?>"
 size="10">
 </td>
 <td>
 <input name="data[<?php echo $i; ?>][libelle]" type="text" value="<?php echo $row['libelle']; ?>"
 size="30">
 </td>
 <td>
 <input name="data[<?php echo $i; ?>][action]" type="text" value="<?php echo $row['action']; ?>"
 size="15">
 </td>
 <td>
 <input name="data[<?php echo $i; ?>][libelle]" type="text" value="<?php echo $row['description']; ?>"
 size="40">
 </td>
 <td>
 <input type="text" name="data[<?php echo $i; ?>][date]" class="date" id="date<?php echo $i; ?>"
 value="<?php 
echo date('d-m-Y',strtotime($row['date_action'])) ;
 ?>" size="12">
 </td>
 <td nowrap>
 <input type="text" name="data[<?php echo $i; ?>][date_validation]" id="data[<?php echo $i; ?>][date_validation]"
 value="<?php if($row['date_validation']=='0000-00-00') {echo 'Non trait&eacute;' ; }
 else {
echo (date('d-m-Y',strtotime($row['date_validation']))); }
 ?>" size="12">
 </td>
 <td nowrap>
 <?php if($row[ 'date_validation']=='0000-00-00' AND strtotime($row[
 'date_action'])>strtotime(date('Y-m-d'))) {?><a href="edit-action.php?id=<?php echo $row['id'] ; ?>&n_doss=<?php echo $row['n_doss'] ; ?>&liasse=<?php echo $_GET['liasse'] ?>&qualite=<?php echo $_GET['qualite'] ?>"><img src="images/gtk-edit.png" width="24" height="24"></a> 
 <a
 href="validate-action.php?id=<?php echo $row['id'] ; ?>&n_doss=<?php echo $row['n_doss'] ; ?>&liasse=<?php echo $_GET['liasse'] ?>&qualite=<?php echo $_GET['qualite'] ?>">
 <img src="images/go.gif" width="24" height="24">
 </a> <a href="delete-action.php?id=<?php echo $row['id'] ; ?>&n_doss=<?php echo $row['n_doss'] ; ?>&liasse=<?php echo $_GET['liasse'] ?>"
 onClick="return confirm('voulez vous vraiment supprimer cette action ?')"><img src="images/trash-paper.png" width="24" height="24"></a>
 <?php
 } elseif($row[ 'date_validation']=='0000-00-00' AND strtotime($row[
 'date_action'])<=strtotime(date( 'Y-m-d'))) {?><a href="#" onClick="return confirm('Vous ne pouvez pas &eacute;diter ou changer la date d\'une action appartenant à votre passif !')"><img src="images/gtk-edit.png" width="24" height="24"></a> 
 <a
 href="validate-action.php?id=<?php echo $row['id'] ; ?>&n_doss=<?php echo $row['n_doss'] ; ?>&liasse=<?php echo $_GET['liasse'] ?>&qualite=<?php echo $_GET['qualite'] ?>">
 <img src="images/go.gif" width="24" height="24">
 </a> <a href="#" onClick="return confirm('Vous ne pouvez pas supprimer une action appartenant à votre passif !')"><img src="images/trash-paper.png" width="24" height="24"></a>
 <?php
 } else { ?>
 <img src="images/tick_48.png" width="24" height="24">
 <?php } ?>
 </td>
 </tr>
 <?php endwhile; ?>
 </td>
 </tr>
 </table>

Here is the form I use for adding days (the form for subtracting days is not done because I yet meet some trouble with the function for adding days.

<table width="50%">
 <tr>
 <td>
 <div class="info"> <strong>Décalage des date </strong>
 <table width="150" border="0" cellspacing="0"
 cellpadding="0">
 <tr>
 <td align="center" bgcolor="#FFFF99">
 <input type="image" src="images/minus_remove_green.png" width="22" height="22"
 name="submit" value="moins" />
 <input name="jours" type="text" value="" size="5" id="nb" />
 <input type="image" src="images/plus_add_green.png" width="22" height="22"
 onClick="addday()" />
 </td>
 </tr>
 </table>Ajouter une action <a href="add-action.php?n_doss=<?php echo $_GET['n_doss'] ; ?>&liasse=<?php echo $_GET['liasse'] ?>&amp;qualite=<?php echo $_GET['qualite'] ; ?>"><img src="images/plus.png" width="24" height="24" border="0"></a>
 </div>
 </td>
 </tr>
</table>

And finally I have this javascript that I've done:

<script type="text/javascript">
 function addday() {
 var items = new Array();
 var itemCount = document.getElementsByClassName("date");
 for (var i = 0; i < itemCount.length; i++) {
 items[i] = document.getElementById("date" + (i + 1)).value;
 }
 for (var i = 0; i < itemCount.length; i++) {
 items[i] = document.getElementById("date" + (i + 1)).value;
 var itemDtParts = items[i].split("-");
 var itemDt = new Date(itemDtParts[2], itemDtParts[1] - 1, itemDtParts[0]);
 nb = document.getElementById('nb').value;
 var newDate = itemDt.getDate() + nb;
 itemDt.setDate(newDate ) ;
 itemCount[i].value = itemDt;
 }
 return items;
 }
</script>​

The thing is that instead of returning good dates it return something weird like that.

Mon Apr 29 2013 00:00:00 GMT+0200 (Paris, Madrid (heure d’été))

I don't know how to return a format like that dd-mm-yyyy

I'm really lost.

I've tried to do that to format itemCount[i].value = itemDt.format("dd-mm-yyyy");

But it returns mistakes saying that date.format is not acceptable for data objects or something like that.

Answer*

Draft saved
Draft discarded
Cancel
4
  • thanks a lot for the help it works almost fine. I think my script has something wrong but I don't know what it is. Actually if I have the following date: 27-08-2012 and I do +1 day it returns me : 28-3-2013 ... If I have a date like 06-09-2012 I do +1 it returns me 31-9-2012 All the fdates are wrong I can not understand why Commented Aug 30, 2012 at 7:36
  • I think when you create your date object you split it for no reason. You .split("-") which would tell me you're using a "-" and not "/", it makes it easier because the Date object can handle ISO format which look like : YYYY-MM-DD. If you would send new Date("2012年08月27日") it would work with a shorter code. Also.. verify your computer's date :-) Commented Aug 30, 2012 at 7:42
  • it is like that when I do echo date('d-m-Y') 30-08-2012 Commented Aug 30, 2012 at 7:44
  • Have you tried something like : var itemDt = new Date(itemDtParts[2] + "-" + itemDtParts[1] + "-" + itemDtParts[0]); Commented Aug 30, 2012 at 7:46

lang-js

AltStyle によって変換されたページ (->オリジナル) /