- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
用这段代码获取ymd:
var ymd = '20190716';
var year = ymd.substring(0, 4);
var month = ymd.substring(4, 2);
var day = ymd.substring(6, 2);
console.log(year);
console.log(month);
console.log(day);
我认为这是从索引到长度获取子字符串的正确方法。所以……
想要:
2019
07
16
但是得到了:
2019
19
1907
为什么?
最佳答案
检查 docs :
The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
和
If indexStart is greater than indexEnd, then the effect of substring() is as if the two arguments were swapped.
所以当您传递 4, 2
或 6, 2
时,您是说您希望结束索引在开始索引,所以参数被切换,你得到索引 2 到 4,索引 2 到 6。
也许您想要 substr
,它可以满足您的期望:
The arguments of substring() represent the starting and ending indexes, while the arguments of substr() represent the starting index and the number of characters to include in the returned string.
var ymd = '20190716';
var year = ymd.substr(0, 4);
var month = ymd.substr(4, 2);
var day = ymd.substr(6, 2);
console.log(year);
console.log(month);
console.log(day);
但它(有点)被弃用了。我更喜欢 substring
或 slice
,并传递索引。
The substring() method swaps its two arguments if indexStart is greater than indexEnd, meaning that a string is still returned. The slice() method returns an empty string if this is the case.
slice
的行为在我看来更直观一些。
关于javascript - 如何使用 JavaScript 通过子字符串获取 ymd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57052839/
我得到了一个里面有一堆日期的文件。它们是十六进制格式,我听说它是 YMD。 例子是:4A273F ..知道我将如何转换它吗?理想化使用 SQL/PLSQL,但我对其他想法持开放态度。 谢谢! 最佳
有人可以帮我将默认系统日期从 ymd 更改为 dmy 吗?这些必须始终是默认格式!怎么办? 最佳答案 SET DATEFORMAT: Sets the order of the month, day,
我正在创建一个表单,用户可以在其中使用 3 个 UITextField 输入生日:1 个代表月份,1 个代表日期,1 个代表年份。我想根据用户的日期格式来排列它们。 即:美国用户为 [ MONTH ]
用这段代码获取ymd: var ymd = '20190716'; var year = ymd.substring(0, 4); var month = ymd.substring(4, 2); v
例如,我的日期为:20171208181856.0Z,我想使用 java 将其转换为日期时间格式。我似乎没有找到任何可行的解决方案。 最佳答案 您可以创建 DateTimeFormatter使用输入格
在 .NET 方面休息了很长时间后,我又花了一些时间在 Java 上。我遇到了这段代码: Date date = new Date(Date.UTC(y - 1900, m - 1, d, h, M,
MySQL DATE_FORMAT 返回空值 SELECT DATE_FORMAT( 'stmt_for', '%Y%m%d' ) FROM `edu_daily_statements` ; 当
我正在寻找一种方法来按优先顺序使用以下“元格式”来解析未知格式的日期: 日-月-年 (DMY) 年-月-日 (YMD) 可能是其他格式(但这并不重要) 这是挪威、丹麦、芬兰和荷兰几乎所有发票上实际使用
我是一名优秀的程序员,十分优秀!