Date is: 10/02/2014 (DD/MM/YYYY). How to convert it into Timestamp format.??
And how to change the format of date.When i write the code like:
var current_date=new Date(); -->i got result like MM/DD/YYYY format.
I want DD/MM/YYYY format for current date.
Date is: 10/02/2014 (DD/MM/YYYY). How to convert it into Timestamp format.??
And how to change the format of date.When i write the code like:
var current_date=new Date(); -->i got result like MM/DD/YYYY format.
I want DD/MM/YYYY format for current date.
Share Improve this question asked Jul 22, 2016 at 11:30 TrojanTrojan 1,4662 gold badges17 silver badges19 bronze badges 1- 10/02/2014 is a timestamp. ;-) – RobG Commented Jul 22, 2016 at 23:56
3 Answers
Reset to default 1Use dateFormat library.
var current_date = new Date();
dateFormat(current_date, "dd/mm/yyyy");
This returns date in "dd/mm/yyyy" format.
By "timestamp" I guess you mean the time value, like 1391954400000
.
To convert a date string to a Date you need to parse it. Use a library or short function. You can then get the time value, which represents a number of milliseconds since 1970-01-01T00:00:00Z:
/* Parse date sting in d/m/y format
** @param {string} s - date string in d/m/y format
** separator can be any non–digit character
** @returns {Date} If s is an invalid date, returned
** Date has time value of NaN (invalid date)
*/
function parseDMY(s) {
var b = s.split(/\D/);
var d = new Date(b[2], --b[1], b[0]);
return d && d.getMonth() == b[1]? d : new Date(NaN);
}
// Valid date
console.log(parseDMY('10/02/2014').getTime());
// Invalid date
console.log(parseDMY('14/20/2016').getTime());
There are also plenty of libraries for formatting dates (e.g. Fecha.js does parsing and formatting), or again, if you have just one format, a simple function will do:
/* Return date string in format dd/mm/yyy
** @param {Date} d - date to format, default to today
** @returns {string} date in dd/mm/yyyy format
*/
function formatDMY(d) {
// Default to today
d = d || new Date();
return ('0' + d.getDate()).slice(-2) + '/' +
('0' + (d.getMonth() + 1)).slice(-2) + '/' +
('000' + d.getFullYear()).slice(-4);
}
console.log(formatDMY());
console.log(formatDMY(new Date(2016,1,29)));
here you are
var current_date=new Date();
var timestamp = current_date.getTime();
var formatted_date = current_date.getDate() + "/" + current_date.getMonth() + 1 + "/" + current_date.getFullYear()
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744363974a4570607.html
评论列表(0条)