javascript - converting time Stamp to date and Date to Time stamp formats - Stack Overflow

Date is: 10022014 (DDMMYYYY).How to convert it into Timestamp format.??And how to change the forma

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
Add a ment  | 

3 Answers 3

Reset to default 1

Use 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信