简体   繁体   English

如何在 typescript 中将字符串转换为日期

[英]How to convert a string into date in typescript

i have a job for converting a string into date in TypeScript我有一份在 TypeScript 中将字符串转换为日期的工作

When i tried to convert "2022-07-01" like "yyyy-MM-dd" by this code当我尝试通过此代码将“2022-07-01”转换为“yyyy-MM-dd”时

  let temp = '2022-07-01';
  let date = new Date(temp);

It returned this value "Fri Jul 01 2022 07:00:00 GMT+0700"它返回了这个值“Fri Jul 01 2022 07:00:00 GMT+0700”
But When my string is "2022-07-01 01" like "yyyy-MM-dd hh".但是当我的字符串是“2022-07-01 01”时,比如“yyyy-MM-dd hh”。 It turned "Invalid Value"它变成了“无效值”
So my question is how can i convert ''yyyy-MM-dd hh" to Date in typeScript and can i change gmt when i convert Date? Thanks所以我的问题是如何在 typeScript 中将“yyyy-MM-dd hh”转换为日期,我可以在转换日期时更改 gmt 吗?谢谢

According to the documentation you're giving a wrongly formatted string to the constructor.根据文档,您向构造函数提供了格式错误的字符串。 You will need to give a ISO 8601 formatted date string.您需要提供一个 ISO 8601 格式的日期字符串。 A date formatted like yyyy-MM-dd hh isn't a ISO 8601 formatted date string.格式为yyyy-MM-dd hh的日期不是 ISO 8601 格式的日期字符串。

Note: When parsing date strings with the Date constructor (and Date.parse, they are equivalent), always make sure that the input conforms to the ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ) — the parsing behavior with other formats is implementation-defined and may not work across all browsers.注意:使用 Date 构造函数(和 Date.parse,它们是等效的)解析日期字符串时,始终确保输入符合 ISO 8601 格式 (YYYY-MM-DDTHH:mm:ss.sssZ) — 解析行为其他格式是实现定义的,可能不适用于所有浏览器。 Support for RFC 2822 format strings is by convention only.仅按惯例支持 RFC 2822 格式字符串。 A library can help if many different formats are to be accommodated.如果要容纳许多不同的格式,图书馆可以提供帮助。

Date-only strings (eg "1970-01-01") are treated as UTC, while date-time strings (eg "1970-01-01T12:00") are treated as local.仅日期字符串(例如“1970-01-01”)被视为 UTC,而日期时间字符串(例如“1970-01-01T12:00”)被视为本地。 You are therefore also advised to make sure the input format is consistent between the two types.因此,还建议您确保两种类型之间的输入格式一致。

Working with date and time can be annoying.使用日期和时间可能很烦人。 Fortunately there are plenty of date helper libraries you can choose from to make your life easier.幸运的是,有很多日期助手库可供您选择,让您的生活更轻松。

Easiest solution is to set a default minutes part最简单的解决方案是设置默认分钟部分

let temp = '2022-07-01 01';
temp += ':00';

let date = new Date(temp);

if you need to change the timezone you must to pass all the date like this:如果您需要更改时区,您必须像这样传递所有日期:

new Date("2015-03-25T12:00:00Z");新日期(“2015-03-25T12:00:00Z”);

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

相关问题 如何通过 TypeScript 将字符串转换为日期 - How to convert string to Date by TypeScript MomentJs:如何在Typescript中将字符串转换为日期? - MomentJs: How to convert string to date in Typescript? 如何将数字数组转换为日期字符串格式。 打字稿/角度7 - How to convert array of numbers into a date string format. Typescript/Angular 7 将 C# dateTime 的字符串转换为 TypeScript 的日期 - convert string of C# dateTime to date of TypeScript Angular2 / Typescript:如何将Javascript / Typescript日期转换为C#DateTime客户端的字符串表示形式 - Angular2/Typescript: How to convert a Javascript/Typescript Date to a String representation of C# DateTime client side 如何将日期格式为“ dd / MM / yyyy”的字符串转换为仅使用打字稿键入日期 - how to convert string of date format 'dd/MM/yyyy' to type date only using typescript 如何将日期格式为“M/d/yyyy”的 Angular/Typescript 字符串转换为 Java 日期格式? - How to convert Angular/Typescript string with date format of 'M/d/yyyy' to Java Date format? 如何在打字稿中将字符串转换为对象 - How to convert string to a object in typescript "打字稿将字符串 [] 转换为字符串" - Typescript convert string[] to string 转换日期格式TypeScript - Convert date format in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM