var mydate = new Date('2014-04-03');console.log(mydate.toDateString());
返回“Wed Apr 02 2014”。我知道这听起来很疯狂,但它会发生在一些用户身上。
防弹溶液如下:
var parts ='2014-04-03'.split('-');// Please pay attention to the month (parts[1]); JavaScript counts months from 0:// January - 0, February - 1, etc.var mydate = new Date(parts[0], parts[1] - 1, parts[2]);console.log(mydate.toDateString());
//get current date in javascript
var currentDate = new Date();
// for getting a date from a textbox as string format
var newDate=document.getElementById("<%=textBox1.ClientID%>").value;
// convert this date to date time
var MyDate = new Date(newDate);
//little bit of code for Converting dates
var dat1 = document.getElementById('inputDate').value;var date1 = new Date(dat1)//converts string to date objectalert(date1);var dat2 = document.getElementById('inputFinishDate').value;var date2 = new Date(dat2)alert(date2);
var a = "13:15"var b = toDate(a, "h:m")//alert(b);document.write(b);
function toDate(dStr, format) {var now = new Date();if (format == "h:m") {now.setHours(dStr.substr(0, dStr.indexOf(":")));now.setMinutes(dStr.substr(dStr.indexOf(":") + 1));now.setSeconds(0);return now;} elsereturn "Invalid Format";}
var ts = '1471793029764';ts = Number(ts); // cast it to a Numbervar date = new Date(ts); // works
var invalidDate = new Date('1471793029764'); // does not work. Invalid Date
function dateDiff(date1, date2){var diff = {} // Initialisation du retourvar tmp = date2 - date1;
tmp = Math.floor(tmp/1000); // Nombre de secondes entre les 2 datesdiff.sec = tmp % 60; // Extraction du nombre de secondes
tmp = Math.floor((tmp-diff.sec)/60); // Nombre de minutes (partie entière)diff.min = tmp % 60; // Extraction du nombre de minutes
tmp = Math.floor((tmp-diff.min)/60); // Nombre d'heures (entières)diff.hour = tmp % 24; // Extraction du nombre d'heures
tmp = Math.floor((tmp-diff.hour)/24); // Nombre de jours restantsdiff.day = tmp;
return diff;
function parseDateTime(datetime) {var monthNames = ["January", "February", "March","April", "May", "June", "July","August", "September", "October","November", "December"];if(datetime.split(' ').length == 3){var date = datetime.split(' ')[0];var time = datetime.split(' ')[1].replace('.00','');var timearray = time.split(':');var hours = parseInt(time.split(':')[0]);var format = datetime.split(' ')[2];var bits = date.split(/\D/);date = new Date(bits[0], --bits[1], bits[2]); /* if you change format of datetime which is passed to this function, you need to change bits e.x ( bits[0], bits[1], bits[2 ]) position as per date, months and year it represent bits array.*/var day = date.getDate();var monthIndex = date.getMonth();var year = date.getFullYear();if ((format === 'PM' || format === 'pm') && hours !== 12) {hours += 12;try{ time = hours+':'+timearray[1]+':'+timearray[2] }catch(e){ time = hours+':'+timearray[1] }}var formateddatetime = new Date(monthNames[monthIndex] + ' ' + day + ' ' + year + ' ' + time);return formateddatetime;}else if(datetime.split(' ').length == 2){var date = datetime.split(' ')[0];var time = datetime.split(' ')[1];var bits = date.split(/\D/);var datetimevalue = new Date(bits[0], --bits[1], bits[2]); /* if you change format of datetime which is passed to this function, you need to change bits e.x ( bits[0], bits[1], bits[2 ]) position as per date, months and year it represent bits array.*/var day = datetimevalue.getDate();var monthIndex = datetimevalue.getMonth();var year = datetimevalue.getFullYear();var formateddatetime = new Date(monthNames[monthIndex] + ' ' + day + ' ' + year + ' ' + time);return formateddatetime;}else if(datetime != ''){var bits = datetime.split(/\D/);var date = new Date(bits[0], --bits[1], bits[2]); /* if you change format of datetime which is passed to this function, you need to change bits e.x ( bits[0], bits[1], bits[2 ]) position as per date, months and year it represent bits array.*/return date;}return datetime;}
var date1 = '2018-05-14 05:04:22 AM'; // yyyy-mm-dd hh:mm:ss Avar date2 = '2018/05/14 05:04:22 AM'; // yyyy/mm/dd hh:mm:ss Avar date3 = '2018/05/04'; // yyyy/mm/ddvar date4 = '2018-05-04'; // yyyy-mm-ddvar date5 = '2018-05-14 15:04:22'; // yyyy-mm-dd HH:mm:ssvar date6 = '2018/05/14 14:04:22'; // yyyy/mm/dd HH:mm:ss
console.log(parseDateTime(date1))console.log(parseDateTime(date2))console.log(parseDateTime(date3))console.log(parseDateTime(date4))console.log(parseDateTime(date5))console.log(parseDateTime(date6))
**Output---**Mon May 14 2018 05:04:22 GMT+0530 (India Standard Time)Mon May 14 2018 05:04:22 GMT+0530 (India Standard Time)Fri May 04 2018 00:00:00 GMT+0530 (India Standard Time)Fri May 04 2018 00:00:00 GMT+0530 (India Standard Time)Mon May 14 2018 15:04:22 GMT+0530 (India Standard Time)Mon May 14 2018 14:04:22 GMT+0530 (India Standard Time)
<!DOCTYPE html><html><head><style></style></head><body><div><span>day:</span><span id='day'></span></div><div><span>month:</span><span id='month'></span></div><div><span>year:</span><span id='year'></span></div><br/><input type="button" id="" value="convert" onClick="convert('/','28/10/1980')"/><span>28/10/1980</span><script>function convert(delimiter,dateString){var splitted = dateString.split('/');// create a new date from the splitted stringvar myDate = new Date(splitted[2],splitted[1],splitted[0]);// now you can access the Date and use its methodsdocument.getElementById('day').innerHTML = myDate.getDate();document.getElementById('month').innerHTML = myDate.getMonth();document.getElementById('year').innerHTML = myDate.getFullYear();}</script></body></html>
var parts ='2014-04-03'.split('-');// Please pay attention to the month (parts[1]); JavaScript counts months from 0:// January - 0, February - 1, etc.var mydate = new Date(parts[0], parts[1] - 1, parts[2]);console.log(mydate.toDateString());