How to create date from locale value string? [duplicate] - javascript

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
How do I create a date from a non-English language?
When using an English formatted string, the date returns a valid date.
var value = "3 mei, 2017 - 11:36";
//var format = 'd MMMM, yyyy - hh:mm';
var d = new Date(value);
When using a non-English formatted string, the date returns an invalid date.
var value = "3 may, 2017 - 11:36";
//var format = 'd MMMM, yyyy - hh:mm';
var d = new Date(value);
What should I change to my code to make the second version run?

I prefer you to use moment.js library.
https://momentjs.com/

Related

How to rearrange date string from MM DD YYYY to YYYY MM DD? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 1 year ago.
I tried new Date(03/11/2015) but it didn't work but new Date(2015/3/11) does. How do I convert a string like '03/11/2015' to '2015/3/11'? I tried using date-fns with the format method but format(new Date(03/15/2015), 'YYYY/MM/DD') returns Invalid time value
Why isn't the Date constructor taking in string values in MM/DD/YYYY format?
You could use a regex replacement approach here:
var input = '03/11/2015';
var output = input.replace(/(\d+)\/(\d+)\/(\d+)/, "$3/$1/$2");
var date = new Date(output);
console.log(date);

How to convert date into specific date format [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
How can I convert string to datetime with format specification in JavaScript?
(15 answers)
Closed 3 years ago.
I am having date which is in mm/yyyy format. I need to compare this date with today's date. I have converted today's date.
After converting I have got date in mm/dd/yyyy format..But I need to covert it into mm/yyyy format..So that I can compare this date into, date which I am getting..Any help please.
selecteddate=05/2019 //which is in mm/yyyy format
myDate= new Date().toLocaleDateString(); //which is in mm/dd/yyyy format( I need to convert this date into mm/yyyy format and need to compare with selecteddate)
Using toLocaleDateString options
let date = new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit' })
console.log(date)
Hope this helps,
const date = new Date();
const myDate = `${(date.getMonth() + 1)}/${date.getFullYear()}`;
console.log(myDate);
You can use Moment.js, as follows:
moment().format('MM/YYYY')
import datetime
selecteddate='05/2019'
selecteddate= datetime.datetime.strptime(selecteddate, '%m/%Y')
today_dtime= datetime.datetime.now()
today_dtime.date()>selecteddate.date()
output:True
One thing note down month and year will assign as in selecteddate,But date will be 1st of respected month.
try this simple trick
You can use the following logic to compare your selected date with today's date
const selectedDate="05/2019"; // MM/YYYY
const today = new Date();
const todayString = `${(today.getMonth() + 1)}/${today.getFullYear()}`;
const matched = ( todayString === todayString );
console.log("Does your selected date matched with today's date ?",(matched ? "Matched":"Don't Matched"));

Invalid Date for some UTC Strings Javascript [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
Trying to get UTC day of the week for any given timestamp on any given machine (w/ their own local time) I used:
var date = new Date(timestamp).toLocaleString('en-GB', { timeZone: 'UTC' });
Once I try to convert the date string to UTC date I get Invalid Date for some dates... it all seems pretty weird.
$ node
> date = new Date('15/08/2019, 00:00:00');
Invalid Date
> date = new Date('12/08/2019, 00:00:00');
2019-12-08T00:00:00.000Z
> date = new Date('15/08/2019');
Any idea where the Invalid Date issue may come from?
By converting the timestamps to strings using the "en-GB" locale, it looks like you're getting them in DD/MM/YYYY format. But in your second example, the strings are being interpreted as "MM/DD/YYYY" in whatever your default locale is, so the first call fails because 15 isn't a valid month number.

How can I convert this date format? [duplicate]

This question already has answers here:
Convert date to specific format in javascript?
(4 answers)
Closed 5 years ago.
An API I use provides a date in the object like 2018-02-14T17:00:00. How can I convert this to make it say: Tuesday, February 14th 7:00 pm
I know how to use .getMonth() methods on a date object but is it possible to do something similar with a string in a date format like this in Javascript?
You can use momentjs to format the date object.
console.log(new moment('2018-02-14T17:00:00').format('dddd, MMMM Do h:mm a'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
You can parse the string into separated values first using String.split() method.
let rawDate = '2018-02-14T17:00:00';
let date = rawDate.split("T")[0]; //2018-02-14
let time = rawDate.split("T")[1]; //17:00:00
let year = date.split("-")[0],
month = date.split("-")[1],
day = date.split("-")[2];
let hr = time.split(":")[0],
mm = time.split(":")[1],
ss = time.split(":")[2];
Now just format these separated values using new Date(year, month, day) etc.
try this
console.log(new moment('2018-02-14T17:00:00').format('LLLL'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
JavaScript natively does not provide a way for you to stringify a date with a provided format.
To do that, you can use moment.js. Here's the specific documentation that says how to do that:
https://momentjs.com/docs/#/displaying/format/

Convert String date YYYY-MM to date in Javascript [duplicate]

This question already has answers here:
convert iso date to milliseconds in javascript
(10 answers)
Closed 7 years ago.
I have a date as a String like : 2015-12 for december 2015.
I would like to convert this date to get this date in milliseconds in javascript. (From the first day of the month)
How can i do it ?
Try using the Date object (new Date()).
You could do :
YourDate + "-01"
And then try to convert to a date
d = new Date(YourDate)
And finaly :
d.getMilliseconds()

Categories

Resources