社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

jquery split get last 2位数

Rushabh Shah • 7 年前 • 2185 次点击  

代码:

'2018-12-2417:25:33'.split('-');

Outptut:

["2018", "12", "2417:25:33"]

预期产量:

["2018", "12", "24"]

有人能告诉我怎样才能达到预期的产量吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/45057
文章 [ 5 ]  |  最新文章 7 年前
Nina Scholz
Reply   •   1 楼
Nina Scholz    7 年前

您可以先将相关部分切片,然后将字符串拆分为多个部分。

var string = '2018-12-2417:25:33',
    result = string.slice(0, 10).split('-');

console.log(result);
Partho63
Reply   •   2 楼
Partho63    7 年前

试试这个:

var date = '2018-12-2417:25:33'.split(':');
date[0].split('-');
Oliver Trampleasure
Reply   •   3 楼
Oliver Trampleasure    7 年前

代码按照您的要求工作,假设您以某种方式将日期和时间代码混合在一起…因此时间码也有8个字符。

我想看看您是如何创建这个字符串的,您应该能够在代码的前面将日期和时间值彼此分离,并避免这种讨厌的黑客攻击。


演示

$(document).on("change keyup paste click", "#dateTime", function() {

  var dateTime = $(this).val();

  // Split on '-'
  var el = dateTime.split('-');

  // Remove the time - we know rimes will always be entered as 'xx:xx:xx' - i.e. 8 characters
  el[2] = el[2].substring(0, el[2].length - 8);

  // Print to console
  $("#dateOutput").text( el );

});

$("#dateTime").click();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<p>You can test out the function below</p>
<p>Remember that it expects an eight character time string at the end of the string</p>
<input id="dateTime" value="2018-12-2417:25:33">
<p id="dateOutput"></p>
Younes Zaidi
Reply   •   4 楼
Younes Zaidi    7 年前

var a = '2018-12-2417:25:33'.split('-');
var result = a[0]+'-'+a[1]+'-'+a[2].substring(0, 2);
console.log(result);
Code Maniac
Reply   •   5 楼
Code Maniac    7 年前

你可以用regex模式 /^\d{4}-\d{2}-\d{2}/ 而不是与 - .

let str = "2018-12-2417:25:33"

let op = str.match(/^\d{4}-\d{2}-\d{2}/)[0].split('-')

console.log(op);

我想你想匹配标准格式 1111-11-11 如果不是,你可以用这个替换上面的regex \d+-\d+-\d{1,2}