在JS中将日期字符串(yymmdd)转换为日期对象 [英] Convert Date String(yymmdd) to Date Object in JS

查看:1612
本文介绍了在JS中将日期字符串(yymmdd)转换为日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期格式为 yymmdd的日期字符串,我想将其转换为JS
中的日期对象,输入字符串为 161208
,我已经尝试了以下代码

I have a date string in "yymmdd" format i want to convert it into date object in JS the input string is "161208" I have tried code below

var mydate = new Date(InputDate);

,但显示无效日期。
这是小提琴
https://jsfiddle.net/4ry0hL4t/

but it says invalid date. Here is the fiddle https://jsfiddle.net/4ry0hL4t/

基本需求是,我有一个日期字符串为 yymmdd格式,我必须将其转换为不同的日期格式,例如( yyyy / mm / dd,yy -mm-dd, yy / mm)。

the basic need is that, I have a date string in "yymmdd" format and i have to convert it to different date formats like ("yyyy/mm/dd, yy-mm-dd","yy/mm").

推荐答案

选中我的答案
基本上,您首先需要为字符串提供正确的格式。您可以手动执行操作,也可以使用moment.js。

Check my answer. Basically you first need to give a proper format to your string. You can either do it manually or use moment.js.

stringFormat = moment(dateObject).format("YYYY-MM-DDTHH:mm:ss");
date = new Date(stringFormat);

确实有助于理解不同的字符串格式如何与不同的浏览器兼容。

This is also really helpful to understand how the different string formats are compatible with different browsers.

这篇关于在JS中将日期字符串(yymmdd)转换为日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆