In JavaScript would it be possible to create a date using a format string?
Example:
new Date('10/12/2020', 'dd/mm/yyyy');
new Date('12/10/2020', 'mm/dd/yyyy');
new Date('2020/01/20', 'yyyy/mm/dd');
new Date('2020-01-20', 'yyyy-mm-dd');
new Date('2020 01 20', 'yyyy mm dd');
All of those would create a new valid date.
Javascript does not support creating date with string format. You can use any of the following libraries for formatting,
moment.js (https://momentjs.com/)
Luxon(https://moment.github.io/luxon/)
date-fns (https://date-fns.org/)
Day.js (https://day.js.org/)
Example for moment.js :
moment().format('MMMM Do YYYY, h:mm:ss a');
moment(Your date).format('MM/DD/YYYY');
Trying to parse a string using the Date constructor shouldn't be done. This can give you unpredictable results in different browser platforms.
The best is to use a popular date library like moment.js.
It is as easy as this.
moment(str, 'YYYY-MM-DD')
Here is the phrasing guide:
https://momentjs.com/guides/#/parsing/
Related
I am getting date data in this format from my API:
"2018-12-26T05:00:29"
however I need to display this in the application front end in a different format like this:
"Monday, Nov 26 at 10:00 am"
How can I achieve this in react-native?
Consider using a third-party library like momentjs for advanced date parsing and formatting. Using moment, you can format the date string as required via the following pattern:
// dddd for full week day, MMM for abreviated month, DD for date, etc
moment(inputDate).format("dddd, MMM DD at HH:mm a")
The momentjs library works well with react-native and can be easily installed by:
npm install moment --save
and imported into your project:
import moment from 'moment';
Here's a snippet demonstrating the pattern shown above:
var inputDate = "2018-12-26T05:00:29";
var outputDate = moment(inputDate).format("dddd, MMM DD at HH:mm a");
console.log(outputDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
While Moment.js can resolve this issue quickly, it's however a pretty big library to use for a single use case. I recommend using date-fns instead. It's lite.
I come out of this thanks to https://github.com/date-fns/date-fns/issues/376#issuecomment-353871093.
Despite the solution, I did some perfection on Date Field Symbol. You may need to use dd instead of DD / yyyy instead of YYYY for formatting days of the month / for formatting years.
Read the doc here: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
I have given a very unusual date format in string. I need to convert it to a JS date object and add up a few days. The last step is clear, but I don't know how to convert the string into the JS date object. Take a look at the string date: October 02, 2016
You should use moment.js
Syntax:
moment(dateString, format, locale)
var dateStr = "Oktober 02, 2016"
var d = moment(dateStr, "MMM DD, YYYY", 'de');
console.log(d.format("DD-MM-YYYY"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment-with-locales.min.js"></script>
Use Date.parse() to parse the date and get the Date object.
var dateObj = new Date('October 02, 2016')
Now, you can perform all the Date operations on dateObj
Given that everything is static here, I thing the best case for you might be to keep a map of your month's name against there number i.e say Oktober : 8. This way you will easily get around of any locale issue in any library.
Once above map is done, you can use .substring to separate your string for blank space and commas to get date and year easily.
Once you have all this you can use new Date constructor with months date and year field.
Hope this all is easily understood, so I am skipping any code here.
Using new Date() can be converted from string to date.
And using setDate() you can add days in the date and can convert the date back to string,
Please check below snippet for more understanding.
var someDate = new Date('October 02, 2016');
console.log(someDate);
console.log(new Date(someDate.setDate(someDate.getDate() + 5)).toString());
How can I generate a date/time with JavaScript in this format:
2015-12-23T15:17:52.000Z
This is an ISO-formatted date, so use toISOString.
new Date().toISOString()
"2015-12-23T15:50:18.134Z"
Just use the new date function.
new Date();
Also, consider taking a look at momentJS for all your javascript date needs.
Hey guys I am retrieving a value from an input box and am using that value to turn into a Date for JavaScript the format is Y-m-d h:i:s. It works perfect in Chrome but any other browser says invalid Date
var old = $(".checked-in-time").val();
old = new Date(old);
UPDATE:
Here is what I am doing:
var current = new Date();
var old = $(".checked-in-time").val();
old = Date.parse(old , 'Y-m-d H:i:s');
var newEnd = current - old
minutes = parseInt((newEnd/(1000*60))%60);
var subtractedWaitTime = minutes;
Pretty much getting the time difference based on minutes.
Date.parse accepts a limited number of formats:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
Your format is not one of the ones supported. You can parse it yourself and just pass the arguments directly. One of the forms Date accepts is this, which would be easy enough to pull out from your format:
new Date(year, month[, date[, hour[, minutes[, seconds[, milliseconds]]]]]);
But I would recommend removing the pain of having to worry about cross browser compatibility and parsing things yourself, and use moment instead, where you can parse the date like this
moment(dateStr,'YYYY-M-D H:m:s')
and then if you needed to have it as a Javascript Date object you could just run
moment().toDate();
in the more likely case you just need to display it formatted somewhere, or compare it to other dates, moment offers many functions for formatting, manipulating and comparing dates
http://momentjs.com/docs/#/displaying/as-javascript-date/
Try one of the following formats
MM-dd-yyyy
yyyy/MM/dd
MM/dd/yyyy
MMMM dd, yyyy
MMM dd, yyyy
Could be that some browsers don't support yyyy-mm-dd
You can parse date like this,
var old = $(".checked-in-time").val();
old = Date.parseDate(old , 'yyyy-mm-dd h:i:s');
if the above not working, you can also try Y-m-d H:i:s this format. For me Y/m/d H:i worked fine.
You could try use such format Y-m-dTH:i:s, e.g. 2011-01-01T12:00:00
Or you can use moment library (Javascript Date library)
I have a date string "Sunday, February 28, 2010" that I would like to convert to a js date object formatted # MM/DD/YYYY but don't know how. Any suggestions?
If you're running with jQuery you can use the datepicker UI library's parseDate function to convert your string to a date:
var d = $.datepicker.parseDate("DD, MM dd, yy", "Sunday, February 28, 2010");
and then follow it up with the formatDate method to get it to the string format you want
var datestrInNewFormat = $.datepicker.formatDate( "mm/dd/yy", d);
If you're not running with jQuery of course its probably not the best plan given you'd need jQuery core as well as the datepicker UI module... best to go with the suggestion from Segfault above to use date.js.
HTH
I would grab date.js or else you will need to roll your own formatting function.
var stringDate = "Sunday, February 28, 2010";
var months = ["January", "February", "March"]; // You add the rest :-)
var m = /(\w+) (\d+), (\d+)/.exec(stringDate);
var date = new Date(+m[3], months.indexOf(m[1]), +m[2]);
The indexOf method on arrays is only supported on newer browsers (i.e. not IE). You'll need to do the searching yourself or use one of the many libraries that provide the same functionality.
Also the code is lacking any error checking which should be added. (String not matching the regular expression, non existent months, etc.)
If you only need it once, it's overkill to load a plugin.
For a date "dd/mm/yyyy", this works for me:
new Date(d.date.substring(6, 10),d.date.substring(3, 5)-1,d.date.substring(0, 2));
Just invert month and day for mm/dd/yyyy, the syntax is new Date(y,m,d)
I used the javascript date funtion toLocaleDateString to get
var Today = new Date();
var r = Today.toLocaleDateString();
The result of r will be
11/29/2016
More info at:
http://www.w3schools.com/jsref/jsref_tolocaledatestring.asp
Use moment js for any date operation.
https://momentjs.com/
console.log(moment("Sunday, February 28, 2010").format('MM/DD/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>