momentjs change string mm/yyyy format to yyyy-mm - javascript

I have a date string 11/2004 that comes to me via an API and on mobile I am trying to display this on a date field.
If i try to render this as it is I see the following warning on console
The specified value "11/2004" does not conform to the required format.
The format is "yyyy-MM" where yyyy is year in four or more digits, and
MM is 01-12.
so using momentjs i thought this should be easy to change to date or month format but i am struggling with it as i cant seem to get the output i want.
I tried the following code but that give me invalid date
moment('11/2004').format('yyyy-MM')
What am i doing wrong here? How do i display the date on an input field in the correct format? I cant change the field type to anything else as its a date picker field on mobile.

Looking at the documentation on parsing what you need to do is created your moment object with the format specified.
moment('11/2004','MM/YYYY')
This will set the day to the first, but the month and the year to the ones specified.
Also, your format is wrong, the yyyy should be capital YYYY
So your final code should look like this
moment('11/2004', 'MM/YYYY').format('YYYY-MM')

You should create your date with the string-format
moment('11/2004', 'MM/YYYY')
and then display it using the format() function
moment('11/2004', 'MM/YYYY').format('YYYY-MM')

Related

How can I check month and day order in mixed date format?

There's column which contains date in different formats (ie. YYYY-MM-DD or YYYY-DD-MM).
When I query it with format time:date, it throw an error: date/time field value out of range: "2022-23-02"
How can i solve it?
How can I check if it's YYYY-MM-DD or YYYY-DD-MM or another?
If you have no field that tells you whether it's YYYY-DD-MM or YYYY-MM-DD, you are kinda out of luck because it's possible that a value could be valid in both formats.
If you are able to redesign, and you must store the date as a string then use YYYY-MM-DD as it's easier for sorting. Optimally, just pass a JavaScript Date object to field of type date, timestamp, or timestampz to the database driver or orm and let it handle the conversion for you. Here's an example with node-postgres: https://node-postgres.com/features/types (the section labeled "date / timestamp / timestamptz")

Is it possible to get week number automatically from datepicker based on user input in Flask form?

I'm totally new in Flask and I really have no idea how to pass only the week number from user input datepicker (year-month-day) into date = request.form.get ("week_number")).
I've spent hours looking for a solution, and I doubt if it is even possible.
Assuming this is your HTML form input type, but if you have other form input types, just play around with the strptime part of this code using the link I attached below, and tune it to your specification
<input name="week_number" type="datetime-local">
You can try stripping the date from the request, like this:
from datetime import datetime
date_input = request.values.get("week_number", default=None)
if date_input is not None and date_input != "":
week_number = datetime.strptime(date_input, "%Y-%m-%dT%H:%M").isocalendar()[1]
Here is a brief explanation of how to use datetime.strptime documentation: https://www.geeksforgeeks.org/python-datetime-strptime-function/
ISO calendar all it does is convert the datetime object to ISO Year, ISO Week Number, and ISO Weekday. [1] allows you to get only the week.

Generate specific date format in JavaScript

I have to generate date in specific format in javascript, The required format is "2017-12-07T14:47:00+0530". I have tried using moments but got stuck in time zone field.
moment().format('YYYY-MM-DDTHH:mm:ssZ'); //output 2018-03-06T14:32:45+05:30
Take a look at the docs
You want the ZZ timezone format rather than a single Z.
console.log(new moment().format('YYYY-MM-DDTHH:mm:ssZZ'))
// 2018-03-06T09:14:44+0000
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>

Convert database date to short date with Javascript

I have a SQL lookup a date that feeds into a field, but the date format contains time, I need to convert it to short date (mm/dd/yyyy). The MSSQL outputs this format (m/d/yyyy 12:00:00 AM) notice that the time is always '12:00:00 AM'. How do I remove the time?
$('#q60').change(function () {
var date = $('#q60 input').val(); //looking up the field that contains the date fed from SQL.
date = date.substring(0, date.indexOf(' '));
});
I have tried using split but while it output the correct thing it doesn't actually change the value in the field for some reason. I have also attempted using the .format similar to this post: Format a date string in javascript
But I am stuck!
with date = date.substring(0, date.indexOf(' ')); you're just storing splitted value in to date variable. to change the value of the input field add $('#q60 input').val(date) at the end of your function.
also in JS there's a whole Date object, with it you can format your date as you please. you can find more about it here and here

Bootstrap datepicker setStartDate not working properly?

I am using Bootstrap datepicker and on selecting different values from a list its startdate is changing. It is working fine if I set the startdate 2013 from 2008 but it doesn't work if a select start date 2008 and currently its 2013.
What could be the reason here?
$('#datepicker').datepicker('setStartDate', updatedDate);
This line I am executing whenever I select different startDate.
Really need to know what updatedDate value is.
However, if you read the docs for the dtepicker the value passed in must be a string that is understandable by format
https://bootstrap-datepicker.readthedocs.io/en/latest/methods.html#setstartdate
https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#startdate
Date or String. Default: Beginning of time
The earliest date that may be selected; all earlier dates will be
disabled.
Date should be in local timezone. String must be parsable with format.
So what you pass in as the format option must match the format of your start date. If you do not set the format optin, the default is "mm/dd/yyyy"
Without seeing code, I can only hypothesize; try calling [...].datepicker('update', 'date_string'); on the object to force an update on the control.

Categories

Resources