JavaScript date: How to add a year to a string that contains mmdd? - Stack Overflow

I have an string that contains monthdate and I need to insert the year. The string looks like:Last Mar

I have an string that contains month/date and I need to insert the year. The string looks like:

Last Mark:: 2/27 6:57 PM

I want to convert the string to something like:

Last Mark:: 2010/02/27 18:57

In this case, there will not be any entries more than a year old. For example, if the date were 10/12 it can be assumed that the year is 2009.

What is the best method for this?

I have an string that contains month/date and I need to insert the year. The string looks like:

Last Mark:: 2/27 6:57 PM

I want to convert the string to something like:

Last Mark:: 2010/02/27 18:57

In this case, there will not be any entries more than a year old. For example, if the date were 10/12 it can be assumed that the year is 2009.

What is the best method for this?

Share Improve this question edited Feb 28, 2010 at 16:10 Daniel Vassallo 345k72 gold badges512 silver badges446 bronze badges asked Feb 28, 2010 at 15:28 user191688user191688 2,6495 gold badges26 silver badges31 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Following from Adam's suggestion:

function convertDate(yourDate) {
   var today = new Date();
   var newDate = new Date(today.getFullYear() + '/' + yourDate);

   // If newDate is in the future, subtract 1 from year
   if (newDate > today) 
       newDate.setFullYear(newDate.getFullYear() - 1);

   // Get the month and day value from newDate
   var month = newDate.getMonth() + 1;
   var day = newDate.getDate();

   // Add the 0 padding to months and days smaller than 10
   month = month < 10 ? '0' + month : month;     
   day = day < 10 ? '0' + day : day;

   // Return a string in YYYY/MM/DD HH:MM format
   return newDate.getFullYear() + '/' +
          month + '/' +
          day + ' ' +
          newDate.getHours() + ':' +
          newDate.getMinutes();
}

convertDate('2/27 6:57 PM');  // Returns: "2010/02/27 18:57"
convertDate('3/27 6:57 PM');  // Returns: "2009/03/27 18:57"

the code for adding THIS year is simple

var d = Date();
var withYear = d.getFullYear() + yourDate;

however, the logic behind considerating if it should be this year or last year could be harder to do

I would think this way: get today's date. If the date is higher than today's, it's last year, so add d.getFullYear()-1, otherwise add d.getFullYear()

This returns the current year:

var d = new Date();
var year = d.getFullYear();

To figure whether its this year or not you could just pare the day and month with the current day and month, and if necessary, subtract 1 from the year.

To get the day and month from the Date object:

d.getMonth(); // warning this is 0-indexed (0-11)
d.getDate();  // this is 1-indexed (1-31)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745302386a4621512.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信