1

I have decided to get the user to enter DDMMYYY to get their birthday. How do I create a UNIX timestamp in javascript from that data?

Also, once I have the unix timestamp, how do I display it in english, i.e. Monday 5th Jan 2000?

I can do it in PHP easily, I just dont know the functions in javascript.

Chud37
  • 4,907
  • 13
  • 64
  • 116

6 Answers6

1

This will be tricky for the input of DDMMYYYY, because if a user misses a digit, or doesn't prefix a singular with a leading 0, it will throw the whole calculation off. The best (and most popular way) for date of birth inputs is 3 drop-down menus for date, month and year. This would then allow you to easily work the date out in a javascript format:

var date = new Date($('year').val(), $('month').val(), $('day').val());

alert(date.toDateString());

This would be better from a UX perspective as it doesn't allow for the user to put an invalid date in, especially for those users from a locale that uses a different date format.

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • yeah, i was reading up on that, and that was my first choice, but as someone pointed out, and i tend to agree - its fiddely and annoying to scroll down a list and select three different values. Where as everyone can type numbers nicely. My plan is to verify the date by calculating it in javascript first and changing a DIV on the fly to show what they are inputting, so and a quick double check of that will confirm to the user that they have done it right. – Chud37 Aug 02 '12 at 11:08
  • Another point if favour of this is that user's don't have to guess what date format to use and there's no place for confusion. Especially is you use a date that has the year at the end (since both DDMMYYYY and MMDDYYY are common). – WhyNotHugo Aug 04 '12 at 00:22
0

You can use substring to get the Day, Month and Year, then create a new Date object in javascript: var ux = new Date(year, month, day).getMilliseconds / 1000;

Rainer.R
  • 458
  • 2
  • 8
0

You need to use some library, or code things yourself, because the built-in date parsing and writing routines of JavaScript are very limited. The formats used there are basically system-dependent.

I’m biased (as the author of “Going Global with JavaScript and Globalize.js”), but I still recommend using Globalize.js, even in cases where you are really not globalizing but use English notations only. After all, using English notations for dates is a form of localization.

Example:

<script src=globalize.js></script>
<script>
var birthDateString = "09Feb1952"; // just a test case
var birthDate = 
   Globalize.parseDate(birthDateString, 'ddMMMyyyy');
if(!birthDate) {
   alert('Error in birth date!'); // replace by suitable error handling
}
else {
   var unixTimeStamp = Math.round(birthDate.getTime()/1000);
   document.write(unixTimeStamp + "<br>");
   document.write(Globalize.format(birthDate, 'dddd d MMM yyyy'));
}
</script>

This would end up with the result “Saturday 9 Feb 1952”, which is not quite of the requested form, so if you really want that form, you would need some added logic that adds the suffix “th”, “st”, “nd”, or “rd”.

Regarding UNIX time stamp vs. JavaScript Date objects, see answers to question How do you get a timestamp in JavaScript?

Community
  • 1
  • 1
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
0

This is pretty easy to do with moment.js.

var birthday = moment("10071986", "DDMMYYYY");
var unix = birthday.unix();
var humanized = birthday.format("dddd Do MMM YYYY");

Here is a working jsfiddle. http://jsfiddle.net/timrwood/rXArM/

timrwood
  • 10,611
  • 5
  • 35
  • 42
0
function dateFromStr(input) {
  var year, month, day;

  year = parseInt(input.substr(4,4));
  month = parseInt(input.substr(2,2));
  day = parseInt(input.substr(0,2));

  return new Date(year, month, day);
}

dateFromStr("03082012");

Cons: a bit verbose. Pros: no external dependencies for something as simple as this.

WhyNotHugo
  • 9,423
  • 6
  • 62
  • 70
-1

Date.js is a great third party library which would allow you to parse the user input in any format, like so :

Without the seperators, it might get a bit tricky to do this. If you are willing to extract the day, month and year Date.js provides elegance thereon:

var dl = new Date(2007, 0, 1);
var unixTimeStamp = dl.valueOf()/1000;
var customDateString = dl.toString('dddd dd MMM yyyy');

See it in action here

Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
  • Date.js has been unmaintained for 5 years. It is buggy and overwrites a lot of native `Date.prototype` methods which make it dangerous to use in production. – timrwood Aug 04 '12 at 00:14
  • Date.js has **awful** design issues, as mentiones above, and is unmaitained. Also, multiple `var` statements is discouraged. – WhyNotHugo Aug 04 '12 at 00:21