To make a time like "2009-05-02 00:00:00" to "2009-05-02".
把“2009-05-02 00:00:00”的时间改为“2009-05-02”。
I know I can achieve this by a regular expression, but is there a built-in function that can do this?
我知道我可以通过正则表达式实现这一点,但是有内置函数可以做到这一点吗?
3 个解决方案
#1
There's no built-in date function that can do that. As a matter of fact if you create a new Date object in JavaScript with that date format, you get an Invalid Date Error.
没有内置的日期功能可以做到这一点。事实上,如果使用该日期格式在JavaScript中创建新的Date对象,则会出现无效日期错误。
You are correct in using a regular expression or string manipulation in this case.
在这种情况下,使用正则表达式或字符串操作是正确的。
Here's a list of all the JavaScript Date Functions.
这是所有JavaScript日期函数的列表。
To simply get the date portion of the string and display it without converting into a Date Object. You can simply do this:
简单地获取字符串的日期部分并显示它而不转换为日期对象。你可以这样做:
var dateString = "2009-05-02 00:00:00"
alert(dateString.substring(0,10)); // Will show "2009-05-02"
To convert this string into a proper JavaScript Date Object, you can use this snippet:
要将此字符串转换为适当的JavaScript日期对象,您可以使用以下代码段:
function sqlTimeStampToDate(timestamp) {
// This function parses SQL datetime string and returns a JavaScript Date object
// The input has to be in this format: 2007-06-05 15:26:02
var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}
The format will be "ddd MMM dd YYYY hh:mm:ss" + TimeOffSet, but you will be able to use any of the standard JavaScript date functions.
格式为“ddd MMM dd YYYY hh:mm:ss”+ TimeOffSet,但您可以使用任何标准的JavaScript日期函数。
#2
See below for two simple methods to get a date format of "2009-05-02", from the initial format, that is "2009-05-02 00:00:00".
请参阅下面的两种简单方法,从初始格式,即“2009-05-02 00:00:00”获取日期格式“2009-05-02”。
<script type="text/javascript">
var mydate, newdate1, newdate2;
mydate = "2009-05-02 00:00:00";
newdate1 = (mydate.split(/ /))[0];
alert('newdate 1: ' + newdate1);
newdate2 = mydate.substr(0,10);
alert('newdate 2: ' + newdate2);
</script>
#3
You might find this helpful:
您可能会发现这有用:
Return today's date and time
How to use the Date() method to get today's date.返回今天的日期和时间如何使用Date()方法获取今天的日期。
getTime()
Use getTime() to calculate the years since 1970.getTime()使用getTime()计算自1970年以来的年份。
setFullYear()
How to use setFullYear() to set a specific date.setFullYear()如何使用setFullYear()设置特定日期。
toUTCString()
How to use toUTCString() to convert today's date (according to UTC) to a string.toUTCString()如何使用toUTCString()将今天的日期(根据UTC)转换为字符串。
getDay()
Use getDay() and an array to write a weekday, and not just a number.getDay()使用getDay()和数组写一个工作日,而不只是一个数字。
This is copy-paste from www.w3schools.com since I can't post a link to it...
这是来自www.w3schools.com的复制粘贴,因为我无法发布链接到它...
Or just search Google for "JavaScript date function" or related. Regular expressions are used to match specific parts of strings, which is useful in searching, extraction and replacement, not really anything that would help you with formatting a date.
或者只是在Google上搜索“JavaScript日期函数”或相关内容。正则表达式用于匹配字符串的特定部分,这在搜索,提取和替换时非常有用,而不是任何可以帮助您格式化日期的内容。
#1
There's no built-in date function that can do that. As a matter of fact if you create a new Date object in JavaScript with that date format, you get an Invalid Date Error.
没有内置的日期功能可以做到这一点。事实上,如果使用该日期格式在JavaScript中创建新的Date对象,则会出现无效日期错误。
You are correct in using a regular expression or string manipulation in this case.
在这种情况下,使用正则表达式或字符串操作是正确的。
Here's a list of all the JavaScript Date Functions.
这是所有JavaScript日期函数的列表。
To simply get the date portion of the string and display it without converting into a Date Object. You can simply do this:
简单地获取字符串的日期部分并显示它而不转换为日期对象。你可以这样做:
var dateString = "2009-05-02 00:00:00"
alert(dateString.substring(0,10)); // Will show "2009-05-02"
To convert this string into a proper JavaScript Date Object, you can use this snippet:
要将此字符串转换为适当的JavaScript日期对象,您可以使用以下代码段:
function sqlTimeStampToDate(timestamp) {
// This function parses SQL datetime string and returns a JavaScript Date object
// The input has to be in this format: 2007-06-05 15:26:02
var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}
The format will be "ddd MMM dd YYYY hh:mm:ss" + TimeOffSet, but you will be able to use any of the standard JavaScript date functions.
格式为“ddd MMM dd YYYY hh:mm:ss”+ TimeOffSet,但您可以使用任何标准的JavaScript日期函数。
#2
See below for two simple methods to get a date format of "2009-05-02", from the initial format, that is "2009-05-02 00:00:00".
请参阅下面的两种简单方法,从初始格式,即“2009-05-02 00:00:00”获取日期格式“2009-05-02”。
<script type="text/javascript">
var mydate, newdate1, newdate2;
mydate = "2009-05-02 00:00:00";
newdate1 = (mydate.split(/ /))[0];
alert('newdate 1: ' + newdate1);
newdate2 = mydate.substr(0,10);
alert('newdate 2: ' + newdate2);
</script>
#3
You might find this helpful:
您可能会发现这有用:
Return today's date and time
How to use the Date() method to get today's date.返回今天的日期和时间如何使用Date()方法获取今天的日期。
getTime()
Use getTime() to calculate the years since 1970.getTime()使用getTime()计算自1970年以来的年份。
setFullYear()
How to use setFullYear() to set a specific date.setFullYear()如何使用setFullYear()设置特定日期。
toUTCString()
How to use toUTCString() to convert today's date (according to UTC) to a string.toUTCString()如何使用toUTCString()将今天的日期(根据UTC)转换为字符串。
getDay()
Use getDay() and an array to write a weekday, and not just a number.getDay()使用getDay()和数组写一个工作日,而不只是一个数字。
This is copy-paste from www.w3schools.com since I can't post a link to it...
这是来自www.w3schools.com的复制粘贴,因为我无法发布链接到它...
Or just search Google for "JavaScript date function" or related. Regular expressions are used to match specific parts of strings, which is useful in searching, extraction and replacement, not really anything that would help you with formatting a date.
或者只是在Google上搜索“JavaScript日期函数”或相关内容。正则表达式用于匹配字符串的特定部分,这在搜索,提取和替换时非常有用,而不是任何可以帮助您格式化日期的内容。