1

I pick this date from a textbox and i would like to format to this format: yyyy-MM-dd So from dd/MM/yyyy to yyyy-MM-dd

 var startDate = document.getElementById('ctl00_PlaceHolderMain_ctl00_Date').value;
    var s = new Date(startDate);
    alert(startDate); //which prints out 7/03/2012
    //when i use the below to try and format it to : yyyy-MM-dd which is what i want
    var scurr_date = s.getDate();
    var scurr_month = s.getMonth();
    scurr_month++;
    var scurr_year = s.getFullYear();

For some reason i get:

var fstartdate = scurr_year + "-" + scurr_month + "-" + scurr_date;
//Output:2012-7-3
instead of : 2012-3-7
also fi i pick a date like 31/12/2011
i get : 2013-7-12

Any ideas what to do.I kind of notice if i use US like 03/07/2012 it kind os works ok. Thank in advance

naijacoder
  • 464
  • 5
  • 14
  • 31

2 Answers2

0

http://www.webdevelopersnotes.com/tips/html/10_ways_to_format_time_and_date_using_javascript.php3

and this

http://www.elated.com/articles/working-with-dates/

Basically, you have 3 methods and you have to combine the strings for yourself:

getDate(): Returns the date
getMonth(): Returns the month
getFullYear(): Returns the year

<script type="text/javascript">
  var d = new Date();
  var curr_date = d.getDate();
  var curr_month = d.getMonth() + 1; //months are zero based
  var curr_year = d.getFullYear();
  document.write(curr_date + "-" + curr_month + "-" + curr_year);
</script>

check this answer link

Community
  • 1
  • 1
Ghostman
  • 6,042
  • 9
  • 34
  • 53
0

You said you want to convert from "dd/MM/yyyy to yyyy-MM-dd". JavaScript's Date constructor will always take the first two digits as a month.

Some regex might help you here:

function fix_date (str) {
    var re = /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
    str = str.replace(re, function (p1, p2, p3, p4) {
        return p4 + '/' + p3 + '/' + p2;        
    });

    return str;
}

var start_date = '7/03/2012';
var new_date = fix_date(start_date);

console.log(new_date); // 2012/03/7​
Kai
  • 9,038
  • 5
  • 28
  • 28
  • Thanks guys.I guess the problem i have is the calendar if i use lcid 3081 which is australia.The date gets all mixed up. If i use lcid :1033 which is US its all fine. Thants why if i pick a date for last year like 31/12/2011 i get the output 2013-7-12 :( The getDate,getMonth etc doesn't pick the right dates,months etc when LCID is 3081 How to fix this – naijacoder Mar 07 '12 at 06:23
  • @kai—*JavaScript's Date constructor will always take the first two digits as a month* - not necessarily. According to [date parsing algorithm in ES5](http://es5.github.com/#x15.9.4.2), it will first try to parse it as an ISO8601 date (which certain browsers fail to do correctly). If that fails, how the string is parsed is impementation dependent. if you want it done correctly in a reasonably cross–browser way, you must break it up and parse it yourself. – RobG Mar 07 '12 at 06:36
  • Thanks Rob i didn't know that and i have been wasting my time. I have decided to use Kai's solution and it worked like a charm. Tha Kai – naijacoder Mar 07 '12 at 06:44
  • Rob to clear this out.How will that work if i had break it up though? The problem is if you select 31/12/2011 its going to pick up 31 as the month.If its 7/03/2012 it will be 7 then its 7.Which means it will only work for US format :( – naijacoder Mar 07 '12 at 06:49