javascript 从时间戳中提取月、日或年

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23104873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:20:27  来源:igfitidea点击:

Extract Month or Day or Year from Timestamp

javascriptdatedate-format

提问by TeO

I would like to know how to get a specific date format (date or month or day or year) from a timestamp. I am wanting to use this in a view with Backbone JS

我想知道如何从时间戳中获取特定的日期格式(日期或月份或日期或年份)。我想在带有 Backbone JS 的视图中使用它

回答by hidden_4003

var d = new Date(1397639141184);
alert(d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear());

回答by Sunil B N

1.If it's JavaScript Timestamp(i.e., in milliseconds)

1.如果是JavaScript Timestamp(即以毫秒为单位)

var date = new Date(13976391000);
var date = date.getDate(); //returns date (1 to 31) you can getUTCDate() for UTC date
var day = date.getMonth(); // returns 1 less than month count since it starts from 0
var year = date.getFullYear(); //returns year 
// You can also use getHours(), getMinutes() and so on

2.If it's database timestamp - example 2013-03-14T02:15:00

2.如果是数据库时间戳 - 例如 2013-03-14T02:15:00

var date = new Date('2013-03-14T02:15:00'); // Works in all browsers
var date = new Date('2013-10-18 08:53:14');// works in Chrome & doesn't work in IE/Mozilla
//note that its a string and you use the same above functions to get Date,month & year