Stata: Convert String to Date - database

I am relatively new to Stata. I have a string variable time that records year and month in the following format:
2000m1
2000m2
2000m3
...
2015m12
I would first like to create a date variable that looks identical (but it has to be in the date format) to the above. Second, I would like to separate year and month components into two different variables, and third, I would like to rename the month component to January, February, etc.
For the first task, the command date = date(time, "YM") returns an empty variable and I can't figure what I am doing wrong.

The function date() yields daily dates, not monthly dates or any other kind of date that isn't a daily date. See its help (help date()) which begins
date(s1,s2[,Y])
Description: the e_d date (days since 01jan1960) corresponding to s1
based on s2 and Y
s1 contains the date, recorded as a string, in virtually
any format. Months can be spelled out, abbreviated (to
three characters), or indicated as numbers; years can
include or exclude the century; blanks and punctuation are
allowed.
s2 is any permutation of M, D, and [##]Y, with their order
defining the order that month, day, and year occur in s1.
##, if specified, indicates the default century for
two-digit years in s1. For instance, s2="MD19Y" would
translate s1="11/15/91" as 15nov1991.
In essence, it needs to be told a day, month and year. You supplied a month and year, and date() won't (can't) play.
As documented at the same place, daily() is a synonym for the same function and it's good practice to use it to remind yourself (and readers of your code) of what it does.
Correspondingly, monthly() provides an easier solution to create a monthly date from string input than in your own answer. Try out solutions using display (di is allowed) on simple cases where you know the right answer.
. di monthly("2000m1", "YM")
480
. di %tm monthly("2000m1", "YM")
2000m1
Reading the documentation is crucial here. See help datetime for a start. There is a lot to explain as dates come in many different forms, but it's all documented.
See also help datetime_display_formats for how to display dates differently. (No "renaming" is involved here.) For example,
. di %tmMonth_CCYY monthly("2000m1", "YM")
January 2000

I figured out the first part. I post the answer to here for anybody who needs a reference:
gen date = ym(real(substr(time, 1,4)),real(substr(time,6,2)))
format date %tm

Try this code may be it works for you
string Date1 = String.Format("{0}", Request.Form["date"]);
string Date2 = String.Format("{0}", Request.Form["date1"]);
Date1 = DateTime.Parse(Date1).ToString("yyyy-MM-dd");
Date2 = DateTime.Parse(Date2).ToString("yyyy-MM-dd");

Related

Power BI: Week #'s not sorting properly

I am having trouble with a line graph visual, where the data is organized by week number and by year number. However when I put the information into the visual and try viewing both 2020 & 2021, it rearranges the data in the order of 2021 & 2020. How do i get it to properly see the data in the correct order of week number by year?
I tried sorting the week # by an index value, also by year, also by week... with no luck
From the images it looks like there is no sort on the year and week, just by the week.
You need to add a column that has a year week key, that you can sort by.
For example 202101 for the week one of 2021.
Assuming you have a date like dd/mm/yyyy format, for example 11/04/2021 in DAX you can use:
YearWeek = YEAR('Table'[Date]) & WEEKNUM('Table'[Date])
This should now sort the data correctly. If you want you can add another column, that is more user friendly like WK01-2021, if you wish, you can then sort by that column, or use the new key column to sort the textual one.
If you just have a year and week column, create a new column that concatenates the two.
For this you should have a Calendar table, that contains a the date groupings that you you need. For example using CALENDARAUTO or you can do it in Power Query here or here.
This actually does not give the correct sequence, when you are dealing with single digit week numbers. For example when dealing with the first ten weeks of 2020, the sequence would be 20201, 202010,20202, 20203... which is obviously wrong.
Here you need a double digit Week number, so a small change in the suggested formula should do it:
YearWeek = YEAR('Table'[Date]) & FORMAT(WEEKNUM('Table'[Date]),"00")
The sequence should now work.

angularjs - confusion with date formats

I am super confused with date formats and need some clarifications. I am trying to pre-populate a form with a date and have set $scope.selectedDate = c.data.Appt.enrolled.start_date;
In my console, c.data.Appt.enrolled.start_date is a string:
However, when I set $scope.selectedDate to that, nothing shows up.
Conversely, if I add new Date in front
(new Date(c.data.Appt.enrolled.start_date))
a date shows up, but it is one day before (April 24, 2018).
In addition to that, when I try to insert the "new Date" version into a function (even though it isn't the correct date), I get a warning in the console saying "Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release."
Can someone explain how I should format the dates so I: 1) get the correct date and not one day prior and 2) am able to plug it into a function without getting that warning?
Thanks!
Since the date is a string with no time zone information (just the date) JavaScript Date parser will treat it as universal(UTC, which is in Greenwich Mean Time) time at 00:00 hours. Then it will subtract the offset of your locale's timezone in hours, and will result in the date being a few hours before or after the day you actually want. This is a common point of confusion.
The best way to solve this is to parse the date manually:
function localDate(dateString) {
var d = dateString.split(/\D/);
return new Date(d[0], d[1]-1, d[2]);
}
See this question for more information: Javascript: parse a string to Date as LOCAL time zone
Moment.js gives that warning because it's considered bad practice to rely on the string parsing that new Date() does since it will have different results in different browsers (IE\Firefox\Etc.). It's more cross-browser friendly to build the date using this form: new Date(year, month, day). (Note that the month starts at zero, not 1)

Representation of repetitive dates in SQL database

In my program I must manage some holidays. Each holiday can be an exact date, or a repetitive date.
For example:
-6th January of each years
-first day of each month of each year
How would you I save this information on db?
I can't use Datetime because with datetime I can't represent the particular cases like in the above example.
Any suggestion?
If it may concern, I am using TSQL
Sorry for my poor english.
The simple approach, without knowing more about your usage of these data, will be to have three numeric fields day, month, year and interpret them this way: If all three are filled, it's exactly that day and no other day. If year isn't filled, it's the day and month, repeating every year. If only the day is filled, it's the day, repeating every month.
If you have more intricate repetition schemes, you'll need an attribute for the repetition rule, like "every three months".
Of course, every query will need to contain some logic to interpret this scheme, but that's inevitable, unless you decide to have a list of all holidays, say the next 50 years.
I would suggest a 3 column design to store this conditions
Column to Specify whether this is a Day or WeekDay
Column to Specify Day/WeekDay Number (ie 1 to 30/31 for Day, 1 to 7 for WeekDay)
Column to Specify Monthly/Yearly occurrence.(means every month/year)
Eg:- For first day of each month,Something like this : DAY,1,MONTHLY
For Second Saturday of every month : WEEKDAY,14*,MONTHLY (*My Week start is Sunday)
This is not a perfect solution, you may have to add some more column to meet your business logic completely.

Short Formula To Pull Upcoming Holidays For a Title Merge In A Huge Database

I am Using excel 2007 and running Windows 7 Pro. I have 50 Thousand rows with data.
I am trying to do a formula that gives me a value of the next upcoming holiday. I have 4 columns of holidays.
For example: Today's date is 6/3/2015 next upcoming holiday would be Father's Day and after that would be Halloween.
So, from Today's date to Father's Day the value would contain "Father's Day"
After Father's day, I want the formula to automatically replace "Father's day" with "Halloween" And doing the same to upcoming holidays after that.
I have researched forums and gotten ideas to compose a formula. I was able to create one, but the only way I got it to work was with the holiday's date instead of there name. If there is no other way of doing it besides that way I would be okay with it.
Only thing i'm concerned about is Opening, Saving, and Editing a file that large with a formula that big is very time consuming and unresponsive sometimes. I need help and would like to have a formula that dose what I need more efficiently and quicker.Any Ideas on shortening the formula?
Here is my formula:
=IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),2,14),"Valentine's Day",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),4,5),"Easter Day",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),5,10),"Mother's Day",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),6,21),"Father's Day",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),10,31),"Halloween",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),12,25),"Christmas"))))))
I have dates in Array A2:D2
I have two differnt ways i can lay out my data. One way is with the holiday's name, and the other is with the holidays date. Examples are shown below... My formula displayed above is created to work with the first example below.
A B C D
2/14/2015 6/21/2015 10/31/2015 12/25/2015
OR
A B C D
Mother's day Father's day Halloween Christmas
I need it to read A-D search for the next upcoming holiday and return the value.
Thanks in advance for any help, tips, and ideas.
If the dates and names for the corresponding holidays are set up as such:
A B
2/14/2015 Valentine's Day
4/5/2015 Easter Day
5/10/2015 Mother's day
6/21/2015 Father's day
10/31/2015 Halloween
12/25/2015 Christmas
This formula can work to get the next holiday from today's date.
=INDEX(B2:B7,MATCH(TODAY(),A2:A7,1)+1)
If the names for the corresponding holidays are not within the list. You can hardcode them in the INDEX formula. Replace the first parameter with an array of hardcoded days, i.e:
{"Valentine's Day","Easter Day","Mother's Day","Father's day","Halloween","Christams"}
Even if we have the dates in a horizontal range, lets say A1:D1 we can still hardcode the days in an INDEX as such:
=INDEX({"Mother's Day","Father's day","Halloween","Christams"},MATCH(TODAY(),$A$1:$D$1,1)+1)

SQL date values converted to integers

Ok, I can't understand this thing.
A customer of mine has a legacy Windows application (to produce invoices) which stores date values as integers.
The problem is that what is represented like '01.01.2002' (value type: date) is indeed stored in SQL Server 2000 as 731217 (column type: integer).
Is it an already known methodology to convert date values into integers (for example - I don't know - in order to make date difference calculations easier?)
By the way, I have to migrate those data into a new application, but for as much I googled about it I can't figure out the algorithm used to apply such conversion.
Can anybody bring some light?
It looks like the number of days since Jan 1st 0000 (although that year doesn't really exists).
Anyway, take a date as a reference like Jan 1st 2000 and look what integer you have for that date (something like 730121).
You then take the difference between the integer you have for a particular date and the one for your reference date and you that number of days to your reference date with the DATEADD function.
DATEADD(day, *difference (eg 731217 - 730121)*, *reference date in proper SQLServer format*)
You can adjust if you're off by a day a two.

Resources