最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery to convert date from mmddyy to yyyy-dd-mm - Stack Overflow

programmeradmin0浏览0评论

I'm receiving an ajax response that has a date in the format mm/dd/yy. How can I convert this to the format yyyy-mm-dd using jquery? Does jquery have an internal function to do this?

I'm receiving an ajax response that has a date in the format mm/dd/yy. How can I convert this to the format yyyy-mm-dd using jquery? Does jquery have an internal function to do this?

Share Improve this question asked Feb 22, 2011 at 22:48 jbluejblue 4,4605 gold badges47 silver badges81 bronze badges 2
  • 3 If you're only getting 2 year digits, how are you going to return 4 if you don't know whether it's 1900's or 2000's? – mVChr Commented Feb 22, 2011 at 22:52
  • 1 @mVChr it's lame but generally you assume that 2-digit numbers greater than something like 90 (depends on the application) are in the 1900s, and 2-digit years smaller than that are in this century. – Pointy Commented Feb 22, 2011 at 22:59
Add a ment  | 

4 Answers 4

Reset to default 4

Simple with no checking of the input (JavaScript, no jQuery):

var d        = '01/25/90';   // as an example
var yy       = d.substr(6,2);
var newdate  = (yy < 90) ? '20' + yy : '19' + yy;
    newdate += '-' + d.substr(0,2) + '-' + d.substr(3,2); //1990-01-25

That sort of thing is not what jQuery is designed to facilitate. The jQuery library is primarily about DOM manipulation, with various concessions made to code structure convenience.

What you need to do is split up the ining address as text and either reconstruct it as a pliant JavaScript parseable date, or else just get the year, month, and day from the string and use the javascript "Date()" constructor that takes those as numeric values.This code will give you a Date from your format:

This code will give you a Date from your format:

function toDate(str) {
  var rv = null;
  str.replace(/^(\d\d)\/(\d\d)\/(\d\d)\$/, function(d, yy, mm, dd) {
    if (!d) throw "Bad date: " + str;
    yy = parseInt(yy, 10);
    yy = yy < 90 ? 2000 + yy : 1900 + yy;
    rv = new Date(yy, parseInt(mm, 10) - 1, parseInt(dd, 10));
    return null;
  });
  return rv;
}
<script>

    $(document).ready(function(){   

        $("#date").change(function(){
            var $d = $(this).val();  
            var $yy = $d.substr(6,4);
            var $dd = $d.substr(3,2);
            var $mm = $d.substr(0,2);
            var $newdate = $yy + '-' + $mm + '-' + $dd; 
            $("#date").val($newdate);
        });
    })

</script>

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Dates</h2>

<p>Output :</p>

<p id="demo"></p>

<script>

const inputval = '121212';
console.log(inputval.value)
const transDtDD =  Number(inputval.slice(0,2))
const transDtMM =  Number(inputval.slice(2,4))
const transDtYY = Number(inputval.slice(4,6))

var dt = new Date();
  dt.setMonth(transDtMM-1);
  
const transDtMMM = dt.toLocaleString('en-US', { month: 'short' })

const dateVal = new Date(transDtDD+'/'+transDtMMM+'/'+transDtYY)

const transDate = dateVal.toLocaleDateString()

document.getElementById("demo").innerHTML = transDate;

</script>

</body>
</html>

I worked out the option to return DD/MM/YYYY from DDMMYY

发布评论

评论列表(0)

  1. 暂无评论