简体   繁体   English

如何从节点js中的日期获取月份和年份

[英]How to get month and year from date in node js

如何在Nodejs获取月份和年份数据并查询以插入数据库?

To get the current month and year you can do the following要获取当前月份和年份,您可以执行以下操作

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

However i cannot answer on how to save to Database since that depends entirely on the Database and Object Modelling you are using.但是我无法回答如何保存到数据库,因为这完全取决于您使用的数据库和对象建模。 Can you provide more info on the Database please.您能否提供有关数据库的更多信息。 Thanks.谢谢。

var months = ["jan", "feb", "mar", "apr", "may", "jun", "july", "aug", "sep", "oct", "nov", "dec"];

var date = new Date();
var month = date.getMonth(); // returns 0 - 11

var year = date.getFullYear();

console.log(months[month]);

console.log(year);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM