yyyy-mm-dd hh:mm:ss aa 的 JavaScript 转换帮助

发布于 2024-08-07 20:21:30 字数 1301 浏览 4 评论 0原文

有人能发现这个函数有什么错误吗? .. 该函数接收 yyyy-mm-dd hh:mm:ss aa 类型的字符串,并将其转换为 UTC 并构建字符串 yyyy-mm-dd hh:mm:ss。

function LocalTimetoUTC(localTime)
{

    var time = localTime.split(" "); //Received :- yyyy-mm-dd hh:mm:ss aa
    var yearday = time[0].split("-");
    var dateTime = time[1].split(":");
    var ampm = time[2];
    var hours = 0;

    var year = yearday[0];
    var month = yearday[1]-1; 
    var day = yearday[2];

    hours = dateTime[0];
    var minutes  = dateTime[1];
    var seconds = dateTime[2];

    /* We have to first convert it to 24 hour format
     * 12:00:00 AM : 00:00:00
     * 12:00:00 PM : 12:00:00 
     * Anytime other than 12
     * 1:00:00 AM   : 1:00:00
     * 1:00:00 PM   : 13:00:00
     */
    if(ampm == "PM")
    {       
        //If it is 12PM, adding 12 will create a problem
        if(hours != 12)
        {
            hours +=12;
        }
    }
    else //AM CASE 
    {   
        if(hours == 12)
        {
            hours = 00;
        }
    }


    var now = new Date(year,month,day,hours,minutes,seconds);

    var utcString = now.getUTCFullYear()+"-"
                  +(now.getUTCMonth()+1)+"-"+now.getUTCDate()+""
                   +now.getUTCHours()+":"+now.getUTCMinutes()+":"+now.getUTCSeconds();

    return utcString;
}

can anybody spot any mistake in this function? .. This is a function which receives a string of type yyyy-mm-dd hh:mm:ss aa and converts to UTC and builds up a string yyyy-mm-dd hh:mm:ss.

function LocalTimetoUTC(localTime)
{

    var time = localTime.split(" "); //Received :- yyyy-mm-dd hh:mm:ss aa
    var yearday = time[0].split("-");
    var dateTime = time[1].split(":");
    var ampm = time[2];
    var hours = 0;

    var year = yearday[0];
    var month = yearday[1]-1; 
    var day = yearday[2];

    hours = dateTime[0];
    var minutes  = dateTime[1];
    var seconds = dateTime[2];

    /* We have to first convert it to 24 hour format
     * 12:00:00 AM : 00:00:00
     * 12:00:00 PM : 12:00:00 
     * Anytime other than 12
     * 1:00:00 AM   : 1:00:00
     * 1:00:00 PM   : 13:00:00
     */
    if(ampm == "PM")
    {       
        //If it is 12PM, adding 12 will create a problem
        if(hours != 12)
        {
            hours +=12;
        }
    }
    else //AM CASE 
    {   
        if(hours == 12)
        {
            hours = 00;
        }
    }


    var now = new Date(year,month,day,hours,minutes,seconds);

    var utcString = now.getUTCFullYear()+"-"
                  +(now.getUTCMonth()+1)+"-"+now.getUTCDate()+""
                   +now.getUTCHours()+":"+now.getUTCMinutes()+":"+now.getUTCSeconds();

    return utcString;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小梨窩很甜 2024-08-14 20:21:30

您的主要问题是您在数字运算中使用字符串。此外,您的输出格式也存在一些问题。这是未经测试的重构:-

function convertToUTCString(localTime)
{
  var dateTimeComponents = localTime.split(" ");
  var dateComponent = dateTimeComponents[0].split("-");
  var timeComponent = dateTimeComponents[1].split(":");
  var ampm = dateTimeComponents[2]

  var dat = new Date(parseInt(dateComponent[0]),
    parseInt(dateComponent[1]) -1,
    parseInt(dateComponent[2]),
    parseInt(timeComponent[0]) % 12,
    parseInt(timeComponent[1]),
    parseInt(timeComponent[2]) );

  if (ampm == "PM")  // do all locales use 'AM' / 'PM' ??
  {
    dat.setHours(dat.getHours() + 12);
  }

  function pad(val)
  {
    var s = val.toString();
    return s.length < 2 ? "0" + s : s
  }

  return dat.getUTCFullYear() + "-" +
    pad((dat.getUTCMonth() + 1 )) + "-" + 
    pad(dat.getUTCDate()) + " " + 
    pad(dat.getUTCHours()) + ":" + 
    pad(dat.getUTCMinutes()) + ":" +
    pad(dat.getUTCSeconds());
}

Your primary problem is you are using strings in numeric operations. In addition your output formatting has some problems too. Here is an untested refactoring:-

function convertToUTCString(localTime)
{
  var dateTimeComponents = localTime.split(" ");
  var dateComponent = dateTimeComponents[0].split("-");
  var timeComponent = dateTimeComponents[1].split(":");
  var ampm = dateTimeComponents[2]

  var dat = new Date(parseInt(dateComponent[0]),
    parseInt(dateComponent[1]) -1,
    parseInt(dateComponent[2]),
    parseInt(timeComponent[0]) % 12,
    parseInt(timeComponent[1]),
    parseInt(timeComponent[2]) );

  if (ampm == "PM")  // do all locales use 'AM' / 'PM' ??
  {
    dat.setHours(dat.getHours() + 12);
  }

  function pad(val)
  {
    var s = val.toString();
    return s.length < 2 ? "0" + s : s
  }

  return dat.getUTCFullYear() + "-" +
    pad((dat.getUTCMonth() + 1 )) + "-" + 
    pad(dat.getUTCDate()) + " " + 
    pad(dat.getUTCHours()) + ":" + 
    pad(dat.getUTCMinutes()) + ":" +
    pad(dat.getUTCSeconds());
}
八巷 2024-08-14 20:21:30

你应该使用 Datejs 来解析日期

Yyou should use Datejs for parsing dates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文