将字符串类型的数据转换为日期 [英] converting string type of data to date
问题描述
我如何使用java脚本或jquery将字符串类型的数据转换为日期,我从很多网站搜索过,但都没有正常工作,我写的如下: >
var actualdate = $(this).find('td')。eq(0).text()。trim(); //即将到来的日期形式12-sep-2014 [所有日期数据库]
var parts = actualdate.split(' - ');
var converteddate = new Date(parts [2],parts [1],parts [0]);
如果我在alert中显示converteddate的值,它显示无效日期!!!
和转换后我应该确定转换日期和今天日期之间的天数,这样如果那些日子是从今天开始30天后我需要显示一些警报信息,请帮我解决这个问题,
(这).find('td')。eq (0).text()。trim(); //以12-sep-2014的形式出现日期[所有日期来自数据库]
var parts = actualdate.split(' - ');
var converteddate = new Date(parts [2],parts [1],parts [0]) ;
如果我在alert中显示converteddate的值,它显示无效日期!!!
和转换后我应该确定转换日期和今天日期之间的天数,这样如果那些日子从今天起30天后越过我需要显示一些警报消息,请帮我解决这个问题,
function customParse(str){
var months = [' Jan', ' 2月',' Mar',' Apr',' 可能',' Jun',
' Jul',' Aug',' Sep',' < span class =code-string> Oct',' Nov',' Dec'],
n = months.length,re = /(\d { 2 }) - ([az] { 3 }) - (\d { 4 })/ i,匹配;
while (n--){months [months [n]] = n; } // 将月份名称映射到其索引:)
matches = str .match(重新); // 从字符串中提取日期部分
返回 new 日期(匹配[ 3 ],月[匹配[ 2 ]],匹配[ 1 ]);
}
此功能将从您的日期字符串返回实际日期,然后您可以比较两个日期
customParse( 19-Aug -2010)> customParse( 18-Aug-2010);
/ * ********您的代码***************** /
var parts = actualdate.split(' - ');
var converteddate = new 日期(部件[ 2 ],部件[ 1 ],部件[ 0 跨度>]);
// 如果我在alert中显示converteddate的值,则表示日期无效 !!!
/ * ======解决方案:
我认为你在上面提到的代码中传递无效的参数,如果你传递它应该工作的有效数据并且我正在测试工作。
* /
var str = 2014\" 年5月6日跨度>;
var dt = new 日期(STR);
// 您可以使用它来获取单独的日期月份和年份。
dt.getDate()+ - +(dt.getMonth ()+1)+ - + dt.getFullYear();
如果有任何问题请告诉我。
- > M.U
how can i convert string type of data into date using java script or jquery, i searched from lots of websites, but all are not working properly, i have written like as follows:
var actualdate = $(this).find('td').eq(0).text().trim();//coming date in the form of 12-sep-2014[all dates from database]
var parts = actualdate.split('-');
var converteddate = new Date(parts[2],parts[1],parts[0]);
if i display the value of converteddate in alert, it is showing "invalid date!!!"
and after conversion i should identify number of days between that converted date and todays date, so that if those days are crossed 30 days from todays date i need to display some alert msg, please help me to solve this,
(this).find('td').eq(0).text().trim();//coming date in the form of 12-sep-2014[all dates from database]
var parts = actualdate.split('-');
var converteddate = new Date(parts[2],parts[1],parts[0]);
if i display the value of converteddate in alert, it is showing "invalid date!!!"
and after conversion i should identify number of days between that converted date and todays date, so that if those days are crossed 30 days from todays date i need to display some alert msg, please help me to solve this,
function customParse(str) { var months = ['Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'], n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches; while(n--) { months[months[n]]=n; } // map month names to their index :) matches = str.match(re); // extract date parts from string return new Date(matches[3], months[matches[2]], matches[1]); }
This function will return the actual date from your date string and then you can compare two dates
customParse("19-Aug-2010") > customParse("18-Aug-2010");
/********* Your code *****************/ var parts = actualdate.split('-'); var converteddate = new Date(parts[2],parts[1],parts[0]); //if i display the value of converteddate in alert, it is showing "invalid date!!!" /*======Solution: I think you are pass invalid arguments in above mention code other wise if you pass valid data it should work and working i have test. */ var str = "05/06/2014"; var dt =new Date(str); //you can use this to get separate date month and year. dt.getDate()+"-"+(dt.getMonth()+1)+"-"+dt.getFullYear();
if any issue then let me know.
-> M.U
这篇关于将字符串类型的数据转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!