I'm using moment.js in my project, what I want is to convert a date in format dd-mm-yyyy to yyyy-mm-dd. I tried this but it didn't work, it gives me an invalid date:
alert(moment('14/10/2017').format('YYYY/MM/DD'));
You need to specify the current format of your date in moment():
var date = moment('14/10/2017', 'DD/MM/YYYY').format('YYYY/MM/DD');
console.log(date); // 2017/10/14
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
Related
I am trying to get date from string in specific format like:
const format = 'MM/dd/yyyy HH:mm';
const dt = "2021-03-11T22:00:00.000Z"; // expecting "03/11/2021 22:00"
console.log(moment(dt).format(format)); // but getting "03/Th/yyyy 23:00"
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
I tried js date, moment, luxon and I don't understand how to make this :(
I suspect that 000Z made problem but this is date I get.
The difference between Moment.js and Luxon is the case of the format keys. Please review the key/token tables.
Moment.js / String + Format
Luxon / Table of tokens
Note: Moment.js has been deprecated in favor of the team's newer library Luxon and other ECMA Script standards that are being introduced in ECMA-402. If you are considering using Moment.js now, just switch over to Luxon.
If you are using Luxon, you can call .toUTC() right before you format the date. Make sure your day of month (dd) and year (yyyy) format keys are lower-case.
const DateTime = luxon.DateTime;
const
format = 'MM/dd/yyyy HH:mm',
dt = "2021-03-11T22:00:00.000Z";
console.log(DateTime.fromISO(dt).toUTC().toFormat(format)); // 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.26.0/luxon.min.js"></script>
If you are using Moment.js, you can call .utc() right before you format the date. Make sure your day of month (DD) and year (YYYY) format keys are upper-case.
const
format = 'MM/DD/YYYY HH:mm',
dt = "2021-03-11T22:00:00.000Z";
console.log(moment(dt).utc().format(format)); // 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Your format string is wrong, it should be: 'MM/DD/YYYY HH:mm'.
Then, if your date comes with ':000Z', you should remove it with the substring() method.
Working code:
const format = 'MM/DD/YYYY HH:mm';
const dateString = "2021-03-11T22:00:00:000Z";
const date = dateString.substring(0, dateString.length - 5);
console.log(moment(date).format(format)); // prints 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
You need to use YYYY instead of yyyy and DD instead of dd to get the expected result.
By the way, I suggest using another library than Moment, like dayjs for instance.
I'm trying to convert below-mentioned date to ISO format(MongoDB)
var d = { storedDate: '26/06/2020 05:55:29 PM' };
I'm however unable to find the parameter that I need to use to get it in the format which I want. I tried the below piece of code.
moment(d.storedDate).format("YYYY-MM-DD HH:mm Z");
How can I get it as ISODate("2020-06-26T17:55:29.274Z")
Please advice
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toISOString() will return ISO date only in UTC that you need for MongoDb. You need to provide the input date format as well.
If you want to store proper Date object, use
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toDate()
You should not store date/time values as strings, use proper data type.
I want to change the format of the date which I entered using date picker
i want to change it dd-mm-yyyy
but now it's in mm/dd/yyyy
If you have date as string, use this to convert in date and then format it
String sDate1="03/08/1993";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
Use this
String pattern = "dd-MM-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(date1);
System.out.println(date);
to format date in any format
Simply use the following lines of code:
$(".datepicker").datepicker({
dateFormat: "dd-mm-yy",
});
In fact, yy serves the purpose of yyyy here.
The backend API I am working with only understands timezone in +0000 format.
I currently I get a date in the current format:
"2017-12-20T16:39:31.000Z"
With moment.js how would I get it in the following string format?
2017-12-20T16:39:31+0000
So far I have done:
var date = new Date();
this.lastCheckedDate = moment(date).toISOString();
You can use the .format() method to format the date in your own way:
moment("2017-12-20T16:39:31.000Z").format('YYYY-MM-DDTHH:mm:ssZZ');
If the date is in another timezone, I recommend using .utc() first to convert the timezone to UTC, and then format the date:
moment("2017-12-20T16:39:31.000+0530").utc().format('YYYY-MM-DDTHH:mm:ssZZ');
I have two inputs, a time and a date input. I'm trying to format them as an ISO string to send to the backend using moment.js.
This is what I have so far 01:00 2016-01-01, I need to format or convert that to ISO. Is there a way to do it using Moment?
To convert ISO I recommend the more standard
date.format();
or
JSON.stringify(yourDate)
or if you prefer momentjs:
var date = moment();
date.toISOString();
or
moment(yourDate).format('MM/DD/YYYY'); // <- your custom format string
To know what are the momentjs formatting rules start reading here
Assuming you are referring to ISO8601 and momentjs (2.10.6), I currently do it like this
var example = momentObject.format("YYYY-MM-DD[T]HH:mm:ss");
You need to use moment's parse function to first create the correct moment object from the data that you have (assuming a 24-hour clock, and month listed before the days):
var myMoment = moment("01:00 2016-01-01", "HH:mm YYYY-MM-DD");
Then you can use moment's format function to output the date in the ISO format that you want. Note that calling the format function without any parameters will output ISO 8601 by default:
myMoment.format();
See the moment docs for more info here.
Hope this helps!