简体   繁体   English

如何在 HTML 中操作字符串?

[英]How do I manipulate string in HTML?

How can I change "2020, 12, 14, 2" to "'2020','12','14','2'" on index.html?如何将 index.html 上的“2020,12,14,2”更改为“'2020','12','14','2'”?

在此处输入图像描述

Thank you for your time,感谢您的时间,

Maybe I Didn't understood your question but in JavaScript you can do也许我不明白你的问题,但在 JavaScript 你可以做

let input = "Dec. 14. 2020. 2 p.m."
let months = ["Jan" , "Feb", "Mar", "Apr", " May", "Jun", "Jul", "Aug", "Sep", "Oct", " Nov", "Dec"]

let d = input.replace(/\./g, "").split(" ")

let output = d[2] + ", " + (months.indexOf(d[0]) + 1) + ", " + d[1] + ", " + d[3]

console.log(output)
//2020, 12, 14, 2

The split method will split up the string into an array of substrings. split方法会将字符串拆分为子字符串数组。 You can then reference those substrings.然后,您可以引用这些子字符串。

Something like:就像是:

 let dateStr = "2020,12,14,2"; let subStrs = dateStr.split(","); // note the use of the spread operator below console.log(...subStrs); // convert to a JS date let date = new Date(...subStrs); console.log(date);

So, as a one-liner:所以,作为一个单行:

 let dateStr = "2020,12,14,2"; // one-liner below let date = new Date(...dateStr.split(",")); console.log(date);

Note that you may need to subtract 1 from the month value since JS is zero-based (months start at 0).请注意,您可能需要从月份值中减去 1,因为 JS 是从零开始的(月份从 0 开始)。

If you need to format more, you can use the getMonth() , getDate() , getYear() , and/or toLocaleDatetring() methods on the Date object.如果您需要更多格式,可以在Date object 上使用getMonth()getDate()getYear()和/或toLocaleDatetring()方法。

{{ dateTime | date:"Y,m,d,H" }}

It looks like Liquid (a server side template language (other examples of those are: smarty, twig). You should look up for a Liquid interpreter and use it.它看起来像Liquid (一种服务器端模板语言(其他示例包括:smarty、twig)。您应该查找 Liquid 解释器并使用它。

Liquid is an open-source template language created by Shopify and written in Ruby. Liquid是由 Shopify 创建并使用 Ruby 编写的开源模板语言。 It is the backbone of Shopify themes and is used to load dynamic content on storefronts.它是 Shopify 主题的主干,用于在店面加载动态内容。 - https://shopify.github.io/liquid/ - https://shopify.github.io/liquid/

They even show a very similar example with date operartor: https://shopify.github.io/liquid/filters/date/他们甚至展示了一个与日期运算符非常相似的示例: https://shopify.github.io/liquid/filters/date/

{{ article.published_at | date: "%a, %b %d, %y" }}

You should use it at the backend and render correct html (without template tags).您应该在后端使用它并呈现正确的 html(没有模板标签)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM