将字符串转换为日期格式(yyyy-MM-dd) [英] converts string into date format(yyyy-MM-dd)

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

问题描述

var fromDate = new Date($("#Date_From").val());
                           var date = new Date(fromDate).toDateString("yyyy-MM-dd");





但它不会将日期更改为所需格式,即yyyy-MM-dd



but it does not changes the date into required format which is yyyy-MM-dd

推荐答案

#Date_From)。val());
var date = new 日期(fromDate).toDateString( yyyy-MM-dd);
("#Date_From").val()); var date = new Date(fromDate).toDateString("yyyy-MM-dd");





但它没有将日期更改为所需的格式,即yyyy-MM-dd



but it does not changes the date into required format which is yyyy-MM-dd


显然,你不明白什么是类型/对象Date的变量。 />


Date或对象类型的变量Date包含表示Date或DateTime的值。这是一个适合比较2个日期的内部格式。

内部格式可以像yyyymmdd。

将日期复制到日期时没有格式转换。



将字符串转换为日期或将日期转换为字符串时的格式转换。



尝试格式化日期变量,同时复制到另一个日期变量是没有意义的。



我建议搜索有关详细信息的教程。
Obviously, you don't understand what is a variable of type/object Date.

A variable of type Date or an object Date contains a value representing a Date or DateTime. This is an internal format suited for comparing 2 Dates.
The internal format can be like yyyymmdd.
There is no format conversion when copying a Date to a Date.

There format conversion on when you convert a String to a Date or a Date to a String.

Trying to format a Date variable while copying to another Date variable is non sense.

I recommend to search for tutorials for more details.


您好,



我不确定您是否可以将特定格式传递给toDateString。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGl obal_Objects%2FDate%2FtoDateString [ ^ ]



我会考虑退房一个名为moment.js的库,用于解析,验证,操作和格式化日期:

http://momentjs.com/ [ ^ ]
Hi,

I am not sure if you can pass a specific format to the toDateString.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate%2FtoDateString[^]

I would consider checking out a library called moment.js for parsing, validating, manipulating, and formatting dates:
http://momentjs.com/[^]


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

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