0

I use a date picker that saves the date in a yyyy-mm-dd format in a database. It then automatically adds time that always appears as 00:00:00. So for example it displays the date like this : 2014-12-14 00:00:00. I want it to display just the date in a mm-dd-yyyy format.

I use the code below but something seems to be wrong with it because it simply doesn't change the way it is displayed. I want to split up each of the values, then display only the date in a different format.

var parts = value.split("/");
return parts[1] + "-" +parts[2] + "-" +parts[0];

What can I do to make it work? Javascript please.

Thanks in advance.

1 Answers1

0

It's a wide variety of solutions. Depends on you server-side settings too, but I will assume, that you use common for most websites MySQL DB and PHP.

  1. You can change you column type to DATE. As said in docs:

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

  1. You can change your column type to VARCHAR and store a date in any format you like when INSERT it
  2. You can convert it to proper format using MySQL built-in DATE_FORMAT()

    SELECT DATE_FORMAT('2014-12-14 00:00:00', '%c-%e-%Y');
    

    will give you 12-14-2014. Here is sqlfiddle, just change first param to your column name in query

  3. On server you can use PHP's date() function like this

    $yourdate = '2014-12-14 00:00:00';
    echo date("m-d-Y", strtotime($yourdate));
    
  4. Closer to your question, how to do it in JS? It also has special object for this: Date(). Put your date in constructor, then use methods:

    var yourdate = new Date('2014-12-14 00:00:00');
    alert(yourdate.getMonth() + "-" + yourdate.getDate() + "-" + yourdate.getFullYear());
    

    JSFiddle < - here

So, as far as almost every platform understands this datetime format - you can use any tool for converting it

vladkras
  • 16,483
  • 4
  • 45
  • 55
  • So if I use the 5th option, I can just directly copy and paste it with out having to change anything? I am bad at picking out how/what to change. @vladkras – Ellen Schlechter Dec 16 '14 at 02:37
  • @EllenSchlechter, yes you can copypaste every example, cause all of them were tested. If you prefer 5th: replace date string with your variable and alert with return – vladkras Dec 16 '14 at 02:49
  • I don't have a variable or any code for that matter unless I need to use part of the code that I originally posted.@vladkras – Ellen Schlechter Dec 16 '14 at 03:44
  • @EllenSchlechter Yep, I'm talking about your code. It looks like your datetime string is stored in variable called "value" and that piece of code must return correct date. That's why you should only replace them in my example: var youtdate = new Date(value); return yourdate.getMonth()... etc. – vladkras Dec 16 '14 at 04:10
  • I don't have the date stored anywhere except in the database so it is not in a variable. I do not how to properly use my code so that is where I am having issues. I am new to programming so this is almost a foreign language. Although I am taking some classes and trying to learn, it is a slow process. :( – Ellen Schlechter Dec 16 '14 at 04:26
  • Then how can I help you if you doesn't even understand what you are doing? You asked how to display date in _different format_. I told you 5 solutions. You said that it's already _displayed_. You cannot _display_ it if you don't have. So your words are contradictory. It's definetely **IN variable** and this variable name is `value` in your example. And you have to work with it or continue learning, cause it's impossible to explain you in a few words the basics you doesn't understand at all. – vladkras Dec 16 '14 at 07:08