Is there a built-in JavaScript function to handle the timeline?
There is no built-in date function that can do this. In fact, if you create a new Date object in JavaScript with this date format, you get an invalid date error.
In this case, you are using regex or string manipulation correctly.
Here is a list of all JavaScript Date Functions .
To just get a part of the date in a string and display it without converting to 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 to the corresponding Java date object, you can use this snippet :
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 can use any of the standard JavaScript date functions.
a source to share
You may find this useful:
Return the date and time of day
How to use the Date () method to get today's date.getTime ()
Use getTime () to calculate years since 1970.setFullYear ()
How to use setFullYear () to set a specific date.toUTCString ()
How to use toUTCString () to convert today's date (according to UTC) to a string.getDay ()
Use getDay () and an array to record the day of the week, not just a number.
It's a copy from www.w3schools.com as I can't post a link to it ...
Or just search Google for "JavaScript date function" or related. Regular expressions are used to match specific parts of strings that are useful in finding, extracting, and replacing, not actually anything that could help you format the date.
Following are two simple methods to get the "2009-05-02" date format, starting with the original format, which is "2009-05-02 00:00:00".
<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>
a source to share