Stanislas Piotrowski
Stanislas Piotrowski

Reputation: 2694

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.

Upvotes: 3

Views: 1717

Answers (1)

Gil Zumbrunnen
Gil Zumbrunnen

Reputation: 1062

If you want to return a nice string with a custom format you will probably be better doing it by hand :

  • Use .getDate() to find the day in the month.
  • Use .getMonth() to find the month's number.
  • Use .getFullYear() to find the year with 4 digits.
  • Add it all in a string.

Now, you declared itemDt = new Date(...) which is fine, but then all you need to do is something like this :

itemDt.setDate(itemDt.getDate()+5)

The Date object will handle months/years itself.

You can use previously stated functions to format your string with something like this :

return itemDt.getDate()+"/"+itemDt.getMonth()+"/"+itemDt.getFullYear() // 15/08/2012

EDIT:

Code above will return 15/8/2012 without zero padding.

return (new Array(2 + 1 - itemDt.getDate().toString().length)).join('0') + itemDt.getDate() + "/" + (new Array(2 + 1 - itemDt.getMonth().toString().length)).join('0') + itemDt.getMonth() + "/" +itemDt.getFullYear();
// returns zero padded dates 09/07/2012

Upvotes: 2

Related Questions