Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2013, 03:19 PM   PM User | #1
juliushg
New Coder

 
Join Date: Jul 2012
Location: Mexico
Posts: 45
Thanks: 9
Thanked 0 Times in 0 Posts
juliushg is an unknown quantity at this point
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;
}
juliushg is offline   Reply With Quote
Old 01-16-2013, 03:25 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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)
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
juliushg (01-16-2013)
Old 01-16-2013, 03:28 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,105
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
juliushg (01-16-2013)
Old 01-16-2013, 03:58 PM   PM User | #4
juliushg
New Coder

 
Join Date: Jul 2012
Location: Mexico
Posts: 45
Thanks: 9
Thanked 0 Times in 0 Posts
juliushg is an unknown quantity at this point
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.
juliushg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:23 AM.


Advertisement
Log in to turn off these ads.