Random Date Js

Welcome to our ultimate guide on the JavaScript Date object and Moment.js. This tutorial will teach you everything you need to know about working with dates and times in your projects.

Useful, free online tool that generates random calendar dates. No ads, nonsense or garbage, just a random date generator. About the random date generator. To pick a date at random, start by specifying the start and end date (day, month, and year) of the period you want to select a random date from. By default the period is one calendar year starting from today. Continue by selecting if.

How to Create a Date Object

Get the current date and time

Get a date and time with individual values

The syntax is Date(year, month, day, hour, minute, second, millisecond).

Note that the months are zero-indexed, beginning with January at 0 and ending with December at 11.

Get a date and time from a timestamp

This represents the time at Thursday, January 1st, 1970 (UTC), or the Unix Epoch time. The Unix Epoch is important because it's what JavaScript, Python, PHP, and other languages and systems use internally to calculate the current time.

new Date(ms) returns the date of the epoch plus the number of milliseconds you pass in. In a day there's 86,400,000 milliseconds so:

will return Friday, January 2nd, 1970 (UTC).

Get a date and time from a string

Getting the date this way is very flexible. All of the examples below return valid Date objects:

You can also use the Date.parse() method to return the number of milliseconds since the epoch (January 1st, 1970):

Setting a time zone

When passing a date string without setting a time zone, JavaScript assumes the date/time are in UTC before converting it to your browser's time zone:

This can lead to errors where the date returned is off by many hours. To avoid this, pass in a time zone along with the string:

You can also pass some, but not all, time zone codes:

Date Object Methods

Often you will not need the entire date, but just part of it like the day, week or month. Fortunately there are a number of methods to do just that:

Make Working with Dates Easier with Moment.js

Getting dates and times right is no small task. Every country seems to have a different way of formatting dates, and accounting for different time zones and daylight savings/summer time takes, well, a whole lot of time. That's where Moment.js shines – it makes parsing, formatting, and displaying dates a breeze.

To start using Moment.js, install it through a package manager like npm, or add it to your site through a CDN. See the Moment.js documentation for more details.

Get the current date and time with Moment.js

This returns an object with the date and time based on your browser's location, along with other locale information. It's similar to native JavaScript's new Date().

Get a date and time from a timestamp with Moment.js

Similar to new Date(ms), you can pass the number of milliseconds since the epoch to moment():

If you want to get a date using a Unix timestamp in seconds, you can use the unix() method:

Get a date and time from a string with Moment.js

Parsing a date from a string with Moment.js is easy, and the library accepts strings in the ISO 8601 or RFC 2822 Date Time format, along with any string accepted by the JavaScript Date object.

ISO 8601 strings are recommended since it is a widely accepted format. Here are some examples:

Setting a time zone with Moment.js

Up until now, we have been using Moment.js in local mode, meaning that any input is assumed to be a local date or time. This is similar to how the native JavaScript Date object works:

However, to set a time zone, you must first get the Moment object in UTC mode:

Then you can adjust for the difference in time zones with the utcOffset() method:

Date

You can also set the UTC offset as a number or a string:

To use named time zones (America/Los_Angeles) or time zone codes (PDT) with Moment objects, check out the Moment Timezone library.

Format the date and time with Moment.js

One of the major strengths that Moment.js has over native JavaScript Date objects is how easy it is to format the output date and time. Just chain the format() method to a Moment date object and pass it a format string as a parameter:

Here's a table with some common formatting tokens:

InputOutputDescription
YYYY20194 digit year
YY192 digit year
MMMMAugustFull month name
MMMAugAbbreviated month name
MM082 digit month
M81 digit month
DDD225Day of the year
DD13Day of the month
Do13thDay of the month with ordinal
ddddWednesdayFull day name
dddWedAbbreviated day name
HH17Hours in 24 hour time
hh05Hours in 12 hour time
mm32Minutes
ss19Seconds
aam / pmAnte or post meridiem
AAM / PMCapitalized ante or post meridiem
ZZ+0900Timezone offset from UTC
X1410715640.579Unix timestamp in seconds
XX1410715640579Unix timestamp in milliseconds

See the Moment.js docs for more formatting tokens.

Working with JavaScript Date objects and Moment.js doesn't have to be time consuming. Now you should know more than enough to get started with both.

Help

List of template tags

random

Date And Time Generator

Returns random item from passed arguments list.

Usage

Returns

*
repeat

Specifies number of repeats of array item. Repeatable array must contains only two items: first is repeat tag, second is item that must be repeated. If no arguments is specified item will be repeated from 0 to 10 times. If min argument is specified, item will be repeated that many times. If both arguments are specified, item will be repeated in specified range of times.

Usage

Arguments

ParamTypeDetails
min (optional)NumberMinimum number in the range. Default is 0.
max (optional)NumberMaximum number in the range. Default is 10.

Returns

Number
index

Index of current cloned object starting from 0.

Usage

Arguments

ParamTypeDetails
startFrom (optional)NumberIndex will start from this value. Default is 0.

Returns

Number
integer

Random integer in specified range. Can be negative.

Usage

Arguments

ParamTypeDetails
min (optional)NumberMinimum number in the range. Default is 0.
max (optional)NumberMaximum number in the range. Default is 10.
format (optional)StringNumber format. For more info visit Numeral.js.

Returns

NumberString
floating

Random float in specified range. If min argument is float, generated number will be float too with same number of decimals. Can be negative.

Usage

Arguments

ParamTypeDetails
min (optional)NumberMinimum number in the range. Default is 0.
max (optional)NumberMaximum number in the range. Default is 10.
fixed (optional)NumberNumber of decimals. Default is 4.
format (optional)StringNumber format. For more info visit Numeral.js.

Returns

NumberString
bool

Random boolean value.

Usage

Returns

Boolean
date

Random date in specified range.

Usage

Arguments

Random Date Scheduler

ParamTypeDetails
min (optional)DateMinimum date in the range. Default is new Date(1970, 0, 1).
max (optional)DateMaximum date in the range. Default is new Date().
format (optional)StringDate format. For more info visit datef.

Returns

Number

Give Me A Random Date

lorem

Random Lorem Ipsum text.

Usage

Arguments

ParamTypeDetails
count (optional)NumberNumber of generated units. Default is 1.
units (optional)StringUnits type. Can be words, sentences, or paragraphs. Default is sentences.

Returns

String
objectId [new]

MongoDB's globally unique identifier for objects.

Usage

Returns

String
guid

Random globally unique identifier.

Usage

Returns

String
custom function

Random Date Java

You can create your own function, that returns any value. this keyword contains current generated object so you can refer previous existing fields as shown in example.

Usage

Arguments

ParamTypeDetails
tagsObjectObject with generation methods which has same names as the tags.
indexNumberIndex of current cloned object starting from 0.

Returns

*
firstName

Random person name of both genders if no gender is specified.

Usage

Arguments

ParamTypeDetails
gender (optional)StringGender of person name. Can be male or female.

Returns

String
gender

Previously generated person gender. Must be after field, that contains firstName tag.

Usage

Returns

String
surname

Random person surname.

Usage

Returns

String
company

Random company name.

Usage

Returns

String
email

Generates email based on firstName, surname and company that are called before of it.

Usage

Arguments

ParamTypeDetails
random (optional)BooleanIf true, random email address will be generated. Default is false.

Returns

String
phone

Generates random phone number.

Random Date Java

Usage

Arguments

ParamTypeDetails
format (optional)StringFormat string which contains x letters. Default is '(xxx) xxx-xxxx'.

Returns

String
country

Random country name.

Usage

Arguments

ParamTypeDetails
abbreviation (optional)BooleanIf passed returns country name abbreviation instead of full name.

Returns

String
countriesList

Returns a list of 205 unique countries instead of passed string.

Usage

Returns

Array
state

Random US state name.

Usage

Arguments

ParamTypeDetails
abbreviation (optional)BooleanIf passed returns state name abbreviation instead of full name.

Returns

String
city

Random US city name.

Usage

Returns

String
street

Random US street name.

Usage

Returns

String

Usage

JSON Generator has a convenient syntax. You can specify JavaScript object for template that you see in the left box. It will be cloned in infinite depth. Fields may have any name and value, but it must be valid JavaScript. Values, which are strings, may contain one or more template tags. When you click 'Generate' the data source object to be copied several times and the place of tags will be inserted random values.

You can copy the generated JSON to clipboard by clicking 'Copy to clipboard'. If you click 'Upload', JSON will be stored on the server and you can download generated file by clicking 'Download' button or access it via ajax-request by URL that will be copied to clipboard after clicking 'Copy URL' button. Yes, JSON Generator can JSONP :) Supported HTTP methods are: GET, POST, PUT, OPTIONS.

Size of uploaded generated files does not exceed 500 kB. Size appears at the top right of the field with the generated data. If file size text is red - file is too large for saving on server, but you can copy it to your clipboard and save locally to *.json file.

You can choose indentation for the generated JSON from the drop-down list. Also when you copy the link to uploaded code there is an 'indent' param in URL. It can has values of 2, 3 and 4. If param is not specified code will be compact. Example: http://www.json-generator.com/j/JSON_ID?indent=4

Also you can choose status code for response by passing 'status' param in URL. Example: http://www.json-generator.com/j/JSON_ID?status=401

JSON Generator remembers your last template in localStorage. If you want to reset it, click 'Reset' button to reset template and UI.

About

We've all faced the problem of lack of information in our app. All interfaces seem lifeless and empty. Debugging with no data is also not simple.

I think that each of us has at least once in their lives created a bunch of files with JSON data, but they were just one object propagated without unique fields. JSON Generator was created in order to help with this. It provides an opportunity generate any data and in any quantity. Edit template, click 'Generate' and you're done.

If you have found a bug, you have a suggestion for improving the application or just want to thank me, click on 'Feedback'.