0

I'm trying to do a really simple formatter, to replace some values in a string I pass in the function. I would like to replace for example 'DD' with the day of week. But when I do it, the return I get is the same string I pass as variable. So my issue is that I don't really know why when I do console log of today(), I get the same string:

today().then(data => console.log(data))

I get the same string that I input, "DD, dd de MM de YY."

const today = () => Promise.resolve(
getDate("DD, dd de MM de YY.").then(data => data)
)

Here is the code which should return my modified text:

const getDate = (text) => { 
    return new Promise( (resolve, reject) => {
        let date = new Date()
        text.replace('DD', translateWeekDay[date.getDay()])
        text.replace('dd', date.getDate().toString())
        text.replace('MM', translateMonth[date.getMonth()])
        text.replace('YY', date.getFullYear().toString())
        resolve(text)
    })
    .catch(err => console.log(err))
}

The thing is I have used a lot promises, so I'm structuring everything like that. Am I missing something essential??

Edit: I used promises because I thought it was something to do with async stuff. I now have seen my dum mistake!

  • What is `myreplace`? I can tell you right now, you're not modifying the original `text` variable, just discarding the return values from the replace functions. – Austin Brunkhorst Jan 08 '18 at 01:04
  • I don't know why a promise is even involved here at all as there is nothing async here, but `.replace()` returns a new string. The original is not modified. You are ignoring the returned string that has the replaced result in it. – jfriend00 Jan 08 '18 at 01:07
  • 1
    Wow... Am I stupid....... I just saw what you said. The funny thing is that I have another file for the same application where I did use replace and it's working. I don't know how I didn't saw it. Too many hours coding I guess! Thank haha – Enrique Foleman Jan 08 '18 at 01:07
  • 1
    You're not stupid by any means. This is what makes programming fun :) – Austin Brunkhorst Jan 08 '18 at 01:08

0 Answers0