I am trying to format a string date to yyyy-mm-dd. Typically, it comes as d/m/yyyy but regardless of the input, I want to have it as yyyy-mm-dd.
const date = new Date("13/10/2016");
const DateParsed = moment(date).format("YYYY-MM-DD");
const dateParsed2 = moment("13/10/2016").format('YYYY-MM-DD')
I was expecting 2016-10-13 but I'm getting 0000-00-00 instead.
Since your input is not in a format recognized by Date.parse, I suggest to use moment(String, String) instead of new Date().
Here a live sample:
const date = "13/10/2016";
const DateParsed = moment(date, 'DD/MM/YYYY').format("YYYY-MM-DD");
console.log(DateParsed);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Related
given the following string: 2022-11-23, how can I use luxon to format it to be 23-11-2022
Using Luxon, you can parse your input string using fromISO (api-docs) and then format to your desired format using toFormat (api-docs) See Table of tokens to get details on how to match part of the datetime with its cooresponding token.
Example:
const DateTime = luxon.DateTime;
console.log(DateTime.fromISO("2022-11-23").toFormat("dd-MM-yyyy"));
<script src="https://cdn.jsdelivr.net/npm/luxon#3.0.4/build/global/luxon.min.js"></script>
In your case, you can also avoid using Luxon and set up some string manipulation like the following:
let parts = "2022-11-23".split('-');
console.log(parts[2] + '-' + parts[1] + '-' + parts[0]);
So I am pulling in a date from MySQL db.
I receive date of "2020-00" for example
I need to compare that to a moment formatted string but moment starts at 1 instead of zero.
How can I use moment to use zero-based formatting?
Bottomline is that I am comparing "2020-01" to "2020-00"
const moment = require('moment');
const string = "2020-01-01"
let tzdate = moment("2020-01-01T00:00:00.000Z")
tzdata = tzdate.format('YYYY-ww');
console.log(tzdata)
I am trying to convert the next date using MomentJS:
const moment = require('moment');
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime);
But it fails and the next message appears:
Deprecation warning: value provided is not in a recognized RFC2822 or
ISO format. moment construction falls back to js Date(), which is not
reliable across all browsers and versions. Non RFC2822/ISO date formats
are discouraged and will be removed in an upcoming major release.
Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more
info.
Snippet showing the issue:
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
I also tried using:
moment.utc(datetime);
but failed.
As moment(String) docs says:
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.
2017-11-19 02:45:22.011 +00:00 is not in ISO 8601 compliant format because there is a space between fractional seconds and UTC offset (2017-11-19 02:45:22.011+00:00 is an ISO 8601 version of your input). So you can use moment(String, String), here a live sample:
var datetime = "2017-11-19 02:45:22.011 +00:00";
var newDate = moment(datetime, 'YYYY-MM-DDTHH:mm:ss.fff Z');
console.log(newDate.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
Reformatting the date string from:
2017-11-19 02:45:22.011 +00:00
To:
2017-11-19T02:45:22.011Z
Solved it
How do I format a date as iso 8601 using moment.js but without the dashes and colons and setting the time to 0 e.g. if I have a date like this:
2016-10-08T09:00:00Z
How do I format as :
20161008T000000Z
Doing moment(date).toISOString() gives 2016-10-08T09:00:00.000Z which is not what I want.
You can simply parse your input into a moment object and use startOf to set time to 00:00:00. Then you can use format method to get a string in your custom format.
Here there is a working example using a string input, you can use the same code also if your input is a javascript Date object.
// Input date as string
var s = '2016-10-08T09:00:00Z';
// Reset time part
// var m = moment(s).startOf('day'); // no UTC
var m = moment.utc(s).startOf('day'); // UTC mode
// Format using custom format
console.log(m.format('YYYYMMDD[T]HHmmss[Z]'));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
My task is to use a datepicker to pick a date in the prescribed format, eg(MM-DD-YYYY) and pass it to the server as ISO formatted.
While it test the output the ISO formatted date is one day behind.
For example
If i select
07-13-2015
My Output ISO format is
ISO format is :2015-07-12T18:30:00.000Z
Here you can see date is 13 but the output date is 12
I am from India. I tried with zone and utcOffset, ended up with no results. How do i set it right
Here is the JSFIDDLE
js code
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(moment(selectedDate).toISOString());
});
I do have a hidden field which value will be updated on change and that will be processed in the server. No issues on that.
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(selectedDate.toISOString());
});
Your selectedDate is already a moment object so you do not need to feed it back into another moment.
Example:
var test = '07-13-2015'
var mtest = moment(test,"MM-DD-YYYY")
mtest.toISOString()
"2015-07-13T06:00:00.000Z"
Your could try converting the date format to UTC at once.
selectedDate = moment(selectedDate).utc('MM-DD-YYYY')
According to http://dygraphs.com/date-formats.html, if you pass a string like '07-13-2015', it means Midnight of 13th July 2015. Now, if you use toISOString function, it will convert it to UTC by default. To not convert it to UTC, just pass a parameter true in the toISOString function. (Moment.js docs)
For example:
var date = '07-13-2015';
date = moment(date,'MM-DD-YYY');
console.log(date.toISOString(true));
This way, moment will not convert the date to UTC.