Javascript 如何从日期对象获取年/月/日?

Javascript 如何从日期对象获取年/月/日?,javascript,date,Javascript,Date,警报(dateObj)发出2009年12月30日星期三00:00:00 GMT+0800 如何以格式获取日期2009/12/30?格式良好的加载项: 这样你就可以写: var now = new Date(); now.format("yyyy/mm/dd"); 使用日期获取方法 由于月份索引是基于0的,所以必须将其增加1 编辑 有关日期对象函数的完整列表,请参见 getMonth() 根据本地时间返回指定日期中的月份(0-11) getUTCMonth() 根据通用时间返回指定日期

警报(dateObj)
发出
2009年12月30日星期三00:00:00 GMT+0800

如何以格式获取日期
2009/12/30

格式良好的加载项:

这样你就可以写:

var now = new Date();
now.format("yyyy/mm/dd");

使用日期获取方法

由于月份索引是基于0的,所以必须将其增加1

编辑

有关日期对象函数的完整列表,请参见

getMonth()
根据本地时间返回指定日期中的月份(0-11)

getUTCMonth()
根据通用时间返回指定日期中的月份(0-11)

var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();

newdate = year + "/" + month + "/" + day;

或者您可以设置新的日期并给出上述值

我建议您使用Moment.js

然后你可以做:

moment(new Date()).format("YYYY/MM/DD");
注意:您实际上不需要添加
newdate()
如果您想要当前的TimeDate,我只添加了它作为引用,您可以向它传递一个日期对象。对于当前时间日期,这也适用于:

moment().format("YYYY/MM/DD");
欧洲(英语/西班牙语)格式 我认为您也需要获取当前日期,您可以使用此日期。

function getFormattedDate(today) 
{
    var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    var day  = week[today.getDay()];
    var dd   = today.getDate();
    var mm   = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
    var hour = today.getHours();
    var minu = today.getMinutes();

    if(dd<10)  { dd='0'+dd } 
    if(mm<10)  { mm='0'+mm } 
    if(minu<10){ minu='0'+minu } 

    return day+' - '+dd+'/'+mm+'/'+yyyy+' '+hour+':'+minu;
}

var date = new Date();
var text = getFormattedDate(date);

输出:星期一-2015年11月16日14:24

new Date().toISOString()
"2016-02-18T23:59:48.039Z"
new Date().toISOString().split('T')[0];
"2016-02-18"
new Date().toISOString().replace('-', '/').split('T')[0].replace('-', '/');
"2016/02/18"

new Date().toLocaleString().split(',')[0]
"2/18/2016"
如果需要两位数的月份和日期(2016/01/01 vs 2016/1/1)

代码

var dateObj = new Date();
var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);
var date = ('0' + dateObj.getDate()).slice(-2);
var year = dateObj.getFullYear();
var shortDate = year + '/' + month + '/' + date;
alert(shortDate);
输出

2016/10/06

小提琴

var dateObj = new Date();
var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);
var date = ('0' + dateObj.getDate()).slice(-2);
var year = dateObj.getFullYear();
var shortDate = year + '/' + month + '/' + date;
alert(shortDate);

信用

来自和的更多信息

更多

var dateObj = new Date();
var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);
var date = ('0' + dateObj.getDate()).slice(-2);
var year = dateObj.getFullYear();
var shortDate = year + '/' + month + '/' + date;
alert(shortDate);

要了解更多关于
.slice
的信息,w3schools帮助我更好地理解了如何使用它。

您只需使用这一行代码即可获得年-月日期格式的日期

var date = new Date().getFullYear() + "-" + new Date().getMonth() + 1 + "-" + new Date().getDate();

我正在使用它,如果你给它传递一个日期obj或js时间戳:

getHumanReadableDate: function(date) {
    if (date instanceof Date) {
         return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
    } else if (isFinite(date)) {//timestamp
        var d = new Date();
        d.setTime(date);
        return this.getHumanReadableDate(d);
    }
}

接受答案后,1月1日将显示如下:
2017/1/1

如果您更喜欢
2017/01/01
,您可以使用:

var dt = new Date();
var date = dt.getFullYear() + '/' + (((dt.getMonth() + 1) < 10) ? '0' : '') + (dt.getMonth() + 1) + '/' + ((dt.getDate() < 10) ? '0' : '') + dt.getDate();
var dt=新日期();
var date=dt.getFullYear()+'/'+((dt.getMonth()+1)<10)?'0':''+(dt.getMonth()+1)+'/'+((dt.getDate()<10)?'0':'')+dt.getDate();

这里有一种更简洁的方法,可以使用模板文本获取年/月/日:

var-date=新日期();
var formattedDate=`${date.getFullYear()}/${(date.getMonth()+1)}/${date.getDate()}`;

console.log(格式化日期)ES2018引入了可用于捕获日、月和年的正则表达式捕获组:

const REGEX = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2});
const results = REGEX.exec('2018-07-12');
console.log(results.groups.year);
console.log(results.groups.month);
console.log(results.groups.day);
const REGEX=/(?[0-9]{4})-(?[0-9]{2})-(?[0-9]{2});
const results=REGEX.exec('2018-07-12');
console.log(results.groups.year);
console.log(results.groups.month);
console.log(results.groups.day);
这种方法的优点是可以捕获非标准字符串日期格式的日、月、年


参考

为什么不将方法
toISOString()
slice
一起使用,或者简单地使用

请点击此处:

const d=new Date()//今天,现在
console.log(d.toISOString().slice(0,10))//YYYY-MM-DD
console.log(d.toLocaleDateString('en-US'))/M/d/YYYY
console.log(d.toLocaleDateString('de-de'))//d.M.YYYY
console.log(d.toLocaleDateString('pt-pt')//DD/MM/YYYY
您可以查看以下详细信息以供参考

new Date().getDate()          // Return the day as a number (1-31)
new Date().getDay()           // Return the weekday as a number (0-6)
new Date().getFullYear()      // Return the four digit year (yyyy)
new Date().getHours()         // Return the hour (0-23)
new Date().getMilliseconds()  // Return the milliseconds (0-999)
new Date().getMinutes()       // Return the minutes (0-59)
new Date().getMonth()         // Return the month (0-11)
new Date().getSeconds()       // Return the seconds (0-59)
new Date().getTime()          // Return the time (milliseconds since January 1, 1970)
let dateObj=new Date();
让myDate=(dateObj.getUTCFullYear())+“/”+(dateObj.getMonth()+1)+“/”+(dateObj.getUTCDate());

console.log(myDate)
2021答案

您可以使用本机
.toLocaleDateString()
函数,该函数支持多个有用的参数,如locale(选择MM/DD/YYYY或YYYY/MM/DD格式)、时区(转换日期)和格式详细信息选项(例如:1 vs 01 vs一月)

示例

new Date().toLocaleDateString() // 8/19/2020

new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}); // 08/19/2020 (month and day with two digits)

new Date().toLocaleDateString('en-ZA'); // 2020/08/19 (year/month/day) notice the different locale

new Date().toLocaleDateString('en-CA'); // 2020-08-19 (year-month-day) notice the different locale

new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); // 8/19/2020, 9:29:51 AM. (date and time in a specific timezone)

new Date().toLocaleString("en-US", {hour: '2-digit', hour12: false, timeZone: "America/New_York"});  // 09 (just the hour)
请注意,有时要以特定的desire格式输出日期,必须找到与该格式兼容的区域设置。 您可以在此处找到区域设置示例:

请注意,区域设置只需更改格式,如果要将特定日期转换为特定国家或城市的等效时间,则需要使用时区参数

它是动态的,它将从用户的浏览器设置中收集语言

使用选项对象中的分钟小时属性来处理它们。。 您可以使用long值来表示月份,如8月23日等


JavaScript日期对象上的月份为零索引。一定要加上1,否则十二月就是十一月;十一月变成了十月forth@Aseem应该是需要+1的月份请记住:一月=0,二月=1,依此类推。
getMonth
getUTCMonth
之间有什么区别?UTC将返回世界时。如果您想使用本地时间,则应将getMonth
getUTCDay()
替换为
getUTCDate()
,因为day是一周中的天数(0-6),date是一个月中的天数(1-31)。我相信此响应仍然存在缺陷。getUTCDate()将返回月份的某一天,但在英格兰。例如,如果我键入:var d=新日期(“1983年7月21日01:15:00”);d、 getDate()返回21,但d.getUTCDate()只返回20这是因为在法国(我所在的地方)凌晨1:15,在英国仍然是23:15。要获取原始日期中的日期,应使用getDate()。您想要UTC日期和时间?需要参数。否则,10月份将显示为第91个月(因为stringland中的9+1=91)应该是dt.getFullYear()+“/”+(dt.getMonth()+1)+“/”+dt.getDate();我认为这对于第三种情况new Date().toISOString().split('T')[0]可能更好;但是如果日期被设置为DeMeEbER,那么它不可能在明年一月到期吗?如果你想添加一个依赖项,这个答案是很好的,如果是2019天的话。JS是一个轻量级的替代MySt.js作为一个考虑的选项。还提到了Luxon和date-fns。问题是具体的
let dateObj = new Date();

let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());
new Date().getDate()          // Return the day as a number (1-31)
new Date().getDay()           // Return the weekday as a number (0-6)
new Date().getFullYear()      // Return the four digit year (yyyy)
new Date().getHours()         // Return the hour (0-23)
new Date().getMilliseconds()  // Return the milliseconds (0-999)
new Date().getMinutes()       // Return the minutes (0-59)
new Date().getMonth()         // Return the month (0-11)
new Date().getSeconds()       // Return the seconds (0-59)
new Date().getTime()          // Return the time (milliseconds since January 1, 1970)
new Date().toLocaleDateString() // 8/19/2020

new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}); // 08/19/2020 (month and day with two digits)

new Date().toLocaleDateString('en-ZA'); // 2020/08/19 (year/month/day) notice the different locale

new Date().toLocaleDateString('en-CA'); // 2020-08-19 (year-month-day) notice the different locale

new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); // 8/19/2020, 9:29:51 AM. (date and time in a specific timezone)

new Date().toLocaleString("en-US", {hour: '2-digit', hour12: false, timeZone: "America/New_York"});  // 09 (just the hour)
function getDate(){
 const now = new Date()
 const option = {
  day: 'numeric',
  month: 'numeric',
  year: 'numeric'
 }
 const local = navigator.language
 labelDate.textContent = `${new 
 Intl.DateTimeFormat(local,option).format(now)}`
}
getDate()