-2

everyone

my problem is that my JS code generates date in format

"Thu Jan 10 2013 00:00:00 GMT+0200 (FLE Standard Time)"

and I need to convert it to format like this

"2013-01-10"

either in JS or PHP, because I'm storing dates in DB in this format. My first thought was to convert it to timestamp, then timestamp convert to new date string, but date or string functions can't read that kind of date format, so propably need to pregreplace some things and thats it.

5
  • Can you post some code please Commented Jan 24, 2013 at 11:13
  • 1
    Have you tried anything? Anything at all? Commented Jan 24, 2013 at 11:13
  • 3
    codepad.viper-7.com/jrujDx Commented Jan 24, 2013 at 11:15
  • @PLB post your code as answer. Commented Jan 24, 2013 at 11:17
  • 1
    @YogeshSuthar I hate posting one line answers and there are three answers already. ;) Also there are hundreds of duplicates of this question. Commented Jan 24, 2013 at 11:19

5 Answers 5

1

Read about php strtotime.

<?php

$date_str = "Thu Jan 10 2013 00:00:00 GMT+0200";

echo date('Y-m-d',strtotime($date_str));
Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example how you can format date in JavaScript:

function formatDate(date) {
   var year = date.getFullYear(),
       month = date.getMonth() + 1,
       day = date.getDate();
   if (month.toString().length === 1) {
      month = '0' + month;
   }
   if (day.toString().length === 1) {
      day = '0' + day;
   }
   return year + '-' + month + '-' + day;
}
formatDate(new Date("Thu Jan 10 2013 00:00:00 GMT+0200 (FLE Standard Time)"));
//"2013-01-10"

Comments

0

You can use this javascript code to get date.

<script type="text/javascript">
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write("" +year + "-" + month + "-" + day + "")
</script>

Comments

0

Try using date.js for conversions.

var myDate = new Date("Thu Jan 10 2013 00:00:00 GMT+0200 (FLE Standard Time)").toString('yyyy-MM-dd');
console.log(myDate); //2013-01-10

Comments

0

You could do something similar to:

var date = new Date("Thu Jan 10 2013 00:00:00 GMT+0200 (FLE Standard Time)"),
    month,
    day,
    date_string;

date_string = date.getFullYear() + '-' +
    ((month = date.getMonth() + 1) < 10 ? '0' + month : month) + '-' +
    ((day = date.getDate()) < 10 ? '0' + day : day);

date_string now holds the value of "2013-01-10".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.