0

I need your help,

how can I rework and restring a date string from yyyy-mm-ddd to dd/mm/yyyy?

Example: 2014-06-27, firstly replace the dash with a slash, then shift the order of the digits around to form 27/06/2014

I am not sure as to how to go about doing this?

Thanks

Jason Kelly
  • 2,539
  • 10
  • 43
  • 80
  • 2
    `output = input.replace(/(\d\d\d\d)-(\d\d)-(\d\d)/,'$3/$2/$1')` - done! ;) – Niet the Dark Absol Jun 23 '14 at 19:28
  • I'd encourage you to try it out yourself. Here are some clues for one solution: http://www.w3schools.com/jsref/jsref_split.asp and http://www.w3schools.com/jsref/jsref_join.asp – Gad Jun 23 '14 at 19:28
  • @NiettheDarkAbsol if you put that into an answer, I will accept. – Jason Kelly Jun 23 '14 at 19:30
  • It's not really answer-worthy, even if it does answer the question. This is the kind of question that's too broad because there's too many possible answers, as levi has just posted one. – Niet the Dark Absol Jun 23 '14 at 19:31
  • 1
    I think it's too bad that when someone asks for `how to go about doing this` everyone just posts direct full code answers instead of helping the user find the solution themselves, especially with a trivial problem... – Gad Jun 23 '14 at 19:36

2 Answers2

1

I've made a custom date string format function, you can use that.

var  getDateString = function(date, format) {
        var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        getPaddedComp = function(comp) {
            return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
        },
        formattedDate = format,
        o = {
            "y+": date.getFullYear(), // year
            "M+": months[date.getMonth()], //month
            "d+": getPaddedComp(date.getDate()), //day
            "h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
             "H+": getPaddedComp(date.getHours()), //hour
            "m+": getPaddedComp(date.getMinutes()), //minute
            "s+": getPaddedComp(date.getSeconds()), //second
            "S+": getPaddedComp(date.getMilliseconds()), //millisecond,
            "t+": (date.getHours() >= 12) ? 'PM' : 'AM'
        };

        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                formattedDate = formattedDate.replace(RegExp.$1, o[k]);
            }
        }
        return formattedDate;
    };

And now suppose you've :-

var date = "2014-06-27";

So to format this date you write:-

var formattedDate = getDateString(new Date(date), "d/M/y")
Indranil Mondal
  • 2,799
  • 3
  • 25
  • 40
0

if you're using a string, then the string.split would be an easy way to do this.

C# code:

public void testDateTime() {

string dashedDate = "2014-01-18"; // yyyy-mm-dd
var stringArray = dashedDate.Split('-');
string newDate = stringArray[2] + "/" + stringArray[1] + "/" + stringArray[0];
//convert to dd/mm/yyyy
Assert.AreEqual(newDate, "18/01/2014");

}

Zman150
  • 1
  • 1