Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts

    What is wrong with this code? (returns text date)

    The objective is to return the value of FechaTexto in the format dd-mm-yyyy, in text, but no matter how I try, it keeps returning "15-1-2013" instead of "15-01-2013". Help, please.

    Code:
    function ObtieneJornada(){
    var dateObject = new Date();
    var WorkingDate = new Date();
    var laHora = dateObject.getHours();
    var DiaTexto = new String();
    var MesTexto1 = new String();
    var MesTexto = new String();
    var AnioTexto = new String();
    var DiaNum = new Number();
    var MesNum = new Number();
    var AnioNum = new Number();
    if (laHora < 9){
    WorkingDate.setDate(dateObject.getDate()-1);
    }
    DiaNum = WorkingDate.getDate();
    MesNum =  WorkingDate.getMonth()+1;
    AnioNum = WorkingDate.getFullYear();
    DiaTexto = DiaNum.toString();
    MesTexto1 = MesNum.toString();
    AnioTexto = AnioNum.toString();
    if (MesTexto1.lenght==1){
    MesTexto="0"+MesTexto1;
    }
    else{
    MesTexto=MesTexto1;
    }
    alert(MesTexto);
    var FechaTexto=DiaTexto.concat("-",MesTexto,"-",AnioTexto);
    return FechaTexto;
    }

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,773
    Thanks
    55
    Thanked 518 Times in 515 Posts
    Code:
    if (MesTexto1.lenght==1){
    looks to me like should be
    Code:
    if (MesTexto1.length==1){
    (me parece que los Latinos siempre tienen problemas con el "th" en inglés)

  • Users who have thanked xelawho for this post:

    juliushg (01-16-2013)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    if (MesTexto1.lenght==1){

    spelling error!

    But better is

    if (MesTextto1 < 10) {MesTextto1 = "0" + MesTextrto1}

    var DiaTexto = new String();
    var MesTexto1 = new String();
    var MesTexto = new String();
    var AnioTexto = new String();
    var DiaNum = new Number();
    var MesNum = new Number();
    var AnioNum = new Number();

    All that is quite unnecessary and redundant. Just declare your variables as e.g.
    var MesNum = WorkingDate.getMonth()+1;

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    juliushg (01-16-2013)

  • #4
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Oh! my bad! I don't have problems with the "th" in english when applied to numbers (fourth, fifth, etc.) it's only with length that sometimes I mispell as lenght.

    Thank you, I will implement the improvements.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •